我使用MSVC 9.0和有此功能:模板類型推演失敗
class RecipientList
{
public:
template<class T>
void fillMessageWithRecipients(typename boost::is_base_of<MsgContent, T>::type* msg);
};
template< class T >
void RecipientList::fillMessageWithRecipients(typename boost::is_base_of<MsgContent, T>::type* msg)
{
// do stuff
}
我想模板類型推演到這裏工作,這樣我就可以這樣使用它:
class SomeMsg : public MsgContent {};
std::auto_ptr<SomeMsg> msg(new SomeMsg);
RecipientList recipients;
recipients.fillMessageWithRecipients(msg.get());
然而我得到的編譯器錯誤:
error C2783: 'void RecipientList::fillMessageWithRecipients(boost::is_base_of::type *)' : could not deduce template argument for 'T'
我有一種感覺,這與事實的類型實際上是p insed是一個指針類型,而不是它自己的類型。任何想法如何我可以正確得到類型扣除在這裏工作?
在此先感謝。
強制性評論:'auto_ptr'已經廢棄了幾個很好的理由。您可能想看看[Boost.SmartPointers](http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/smart_ptr.htm) – pmr
C++ 11也提供了smartpointres。 [見維基](http://en.wikipedia.org/wiki/Smart_pointer#C.2B.2B_smart_pointers)。這裏不需要提升。 – ManuelSchneid3r
@pmr從你的鏈接:「這些模板是爲了補充std :: auto_ptr模板而設計的。」,在移植到C++ 11之外沒有替代auto_ptr(我不能這樣做,我更喜歡在那裏使用'unique_ptr')。 –