Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板类型扣除失败_C++_Templates_Boost_Template Meta Programming - Fatal编程技术网

C++ 模板类型扣除失败

C++ 模板类型扣除失败,c++,templates,boost,template-meta-programming,C++,Templates,Boost,Template Meta Programming,我使用的是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::fillMessageWithRecipie

我使用的是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
}
类接收者列表
{
公众:
模板
void fillMessageWithRecipients(typename boost::is_base_of::type*msg);
};
模板
void RecipientList::fillMessageWithRecipients(类型名boost::is_base_of::type*msg)
{
//做事
}
我希望模板类型推断在这里起作用,因此我可以这样使用它:

class SomeMsg : public MsgContent {};

std::auto_ptr<SomeMsg> msg( new SomeMsg );

RecipientList recipients;
recipients.fillMessageWithRecipients( msg.get() );
class-SomeMsg:public-MsgContent{};
std::自动ptr消息(新消息);
收件人名单;
recipients.fillMessageWithRecipients(msg.get());
但是,我得到了编译器错误:

错误C2783:“无效” RecipientList::fillMessageWithRecipients(boost::is_base_of::type *)“:无法推断“T”的模板参数”

我觉得这与实际传入的类型是指针类型,而不是类型本身有关。你知道我怎样才能在这里正确地进行类型推断吗


提前谢谢。

我感觉你误用了
boost::is\u base\u of
。嵌套的
类型
将是
true\u类型
false\u类型
。这两个都不能作为论点,你的指针也不能转换成这些论点

你真正想要的是:

#include <boost/type_traits/is_base_of.hpp>
#include <boost/utility/enable_if.hpp>

class MsgContent {};

class RecipientList
{
public:
    template<class T>
    typename boost::enable_if<
        typename boost::is_base_of<MsgContent, T>::type
      , void>::type
    fillMessageWithRecipients(T* t) { }
};

class SomeMsg : public MsgContent {};

int main()
{
  RecipientList recipients;
  SomeMsg m;
  recipients.fillMessageWithRecipients( &m );

  return 0;
}
#包括
#包括
类MsgContent{};
类接收者列表
{
公众:
模板
typename boost::如果<
typename boost::是::type的\u base\u吗
,void>::类型
fillMessageWithRecipients(T*T){}
};
类SomeMsg:publicmsgcontent{};
int main()
{
收件人名单;
SomeMsg m;
recipients.fillMessageWithRecipients(&m);
返回0;
}

我感觉你误用了
boost::is\u base\u of
。嵌套的
类型
将是
true\u类型
false\u类型
。这两个都不能作为论点,你的指针也不能转换成这些论点

你真正想要的是:

#include <boost/type_traits/is_base_of.hpp>
#include <boost/utility/enable_if.hpp>

class MsgContent {};

class RecipientList
{
public:
    template<class T>
    typename boost::enable_if<
        typename boost::is_base_of<MsgContent, T>::type
      , void>::type
    fillMessageWithRecipients(T* t) { }
};

class SomeMsg : public MsgContent {};

int main()
{
  RecipientList recipients;
  SomeMsg m;
  recipients.fillMessageWithRecipients( &m );

  return 0;
}
#包括
#包括
类MsgContent{};
类接收者列表
{
公众:
模板
typename boost::如果<
typename boost::是::type的\u base\u吗
,void>::类型
fillMessageWithRecipients(T*T){}
};
类SomeMsg:publicmsgcontent{};
int main()
{
收件人名单;
SomeMsg m;
recipients.fillMessageWithRecipients(&m);
返回0;
}

您应该同时使用is\u base\u of

is_base_本身就是谓词


你应该用is_base_of来搭配

is_base_本身就是谓词


强制性注释:
auto_ptr
由于一些好的原因被弃用。您可能还想看看C++11提供的smartpointres。这里不需要boost。@pmr来自您的链接:“这些模板是为了补充std::auto_ptr模板而设计的。”,除了迁移到C++11之外,没有任何东西可以替代auto_ptr(我不能这样做,我更喜欢在那里使用
unique_ptr
)@RobertDailey
boost::scoped_ptr
唯一的_ptr
,没有移动语义。这里看起来很合适。@pmr我的示例是为了简单而设计的。在我的真实代码中,我实际上需要移动语义。不过,我完全了解其他智能指针选择。出于几个好的原因,不推荐使用强制注释:
auto\u ptr
。您可能还想看看C++11提供的smartpointres。这里不需要boost。@pmr来自您的链接:“这些模板是为了补充std::auto_ptr模板而设计的。”,除了迁移到C++11之外,没有任何东西可以替代auto_ptr(我不能这样做,我更喜欢在那里使用
unique_ptr
)@RobertDailey
boost::scoped_ptr
唯一的_ptr
,没有移动语义。这里看起来很合适。@pmr我的示例是为了简单而设计的。在我的真实代码中,我实际上需要移动语义。不过,我完全了解其他智能指针选择。不过,谢谢,
SomeMsg m
必须是指针类型,而不是引用:(@RobertDailey在我的例子中,它是一个指针。看看
&
。这肯定有效,谢谢;有点尴尬,我忘记了如何使用类型特征:PThanks,但是,
SomeMsg m
必须是指针类型,而不是引用:(@RobertDailey在我的例子中是一个指针。看看
&
。这肯定有效,谢谢;有点尴尬,我忘记了如何使用类型特征:P
Base1
Base2