小编Chr*_*ris的帖子

用作模板化函数输入的函数的void返回值被视为参数

假设您有一些目标类,其中包含一些方法:

class Subject
{
public:
  void voidReturn() { std::cout<<__FUNCTION__<<std::endl; }
  int  intReturn()  { std::cout<<__FUNCTION__<<std::endl; return 137; }
};
Run Code Online (Sandbox Code Playgroud)

和Value类(概念类似于Boost.Any):

struct Value
{
  Value() {}
  Value( Value const & orig ) {}
  template< typename T > Value( T const & val ) {}
};
Run Code Online (Sandbox Code Playgroud)

我想使用Subject类中的方法生成一个Value对象:

Subject subject;
Value intval( subject.intReturn() );
Value voidVal( subject.voidReturn() );  // compilation error
Run Code Online (Sandbox Code Playgroud)

我在VC++ 2008中遇到以下错误:

error C2664: 'Value::Value(const Value &)' : cannot convert parameter 1 from 'void' to 'const Value &'
Expressions of type void cannot …
Run Code Online (Sandbox Code Playgroud)

c++ templates generic-programming

6
推荐指数
1
解决办法
1351
查看次数

标签 统计

c++ ×1

generic-programming ×1

templates ×1