【发布时间】:2012-10-30 03:55:37
【问题描述】:
我正在使用 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() );
但是我得到编译器错误:
错误 C2783: '无效 RecipientList::fillMessageWithRecipients(boost::is_base_of::type *)' : 无法推导出 'T' 的模板参数
我感觉这与实际传入的类型是指针类型而不是类型本身有关。知道如何在这里正确地进行类型推导吗?
提前致谢。
【问题讨论】:
-
强制性评论:
auto_ptr已被弃用,原因有几个。你可能想看看Boost.SmartPointers -
C++11 也提供了智能点。 See Wiki。这里不需要提升。
-
@pmr 来自您的链接:“这些模板旨在补充 std::auto_ptr 模板。”,除了迁移到 C++11(我不能做,我更喜欢在那里使用
unique_ptr)。 -
@RobertDailey
boost::scoped_ptr是unique_ptr没有移动语义。在这里看起来很合适。 -
@pmr 我的示例是为简单起见而设计的。在我的真实代码中,我实际上需要移动语义。不过,我完全了解其他智能指针选择。
标签: c++ templates boost template-meta-programming