7

假设你有一些目标类,上面有一些方法:

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

还有一个 Value 类(在概念上类似于 Boost.Any):

struct Value
{
  Value() {}
  Value( Value const & orig ) {}
  template< typename T > Value( T const & val ) {}
};

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

Subject subject;
Value intval( subject.intReturn() );
Value voidVal( subject.voidReturn() );  // compilation error

在 VC++2008 中出现以下错误:

error C2664: 'Value::Value(const Value &)' : cannot convert parameter 1 from 'void' to 'const Value &'
Expressions of type void cannot be converted to other types

和 gcc 4.4.3:

/c/sandbox/dev/play/voidreturn/vr.cpp:67: error: invalid use of void expression

上下文是当您想在模板类中使用它时:

template< typename Host, typename Signature > class Method;

// Specialization for signatures with no parameters
template< typename Host, typename Return >
class Method< Host, Return () >
{
public:
  typedef Return (Host::*MethodType)();
  Method( Host * host, MethodType method ) : m_Host(host), m_Method(method) {}

  Value operator()() { return Value( (m_Host->*m_Method)() ); }
private:
  Host       * m_Host;
  MethodType   m_Method;
};

在返回某些内容(即 intReturn)的方法上使用此 Method 类将如下所示:

Method< Subject, int () > intMeth( &subject, &Subject::intReturn );
Value intValue = intMeth();

但是,使用 voidReturn 方法执行此操作:

Method< Subject, void () > voidMeth( &subject, &Subject::voidReturn );
Value voidValue = voidMeth();

产生与上述类似的错误。

一种解决方案是进一步对 void 返回类型的方法进行部分专门化:

template< typename Host >
class Method< Host, void () >
{
public:
  typedef void Return;
  typedef Return (Host::*MethodType)();
  Method( Host * host, MethodType method ) : m_Host(host), m_Method(method) {}

  Value operator()() { return (m_Host->*m_Method)(), Value(); }
private:
  Host       * m_Host;
  MethodType   m_Method;
};

除了感觉难看之外,我还想将 Method 类专门用于 X 个签名参数,这已经涉及大量代码重复(希望 Boost.Preprocessor 可以在这里提供帮助),然后为 void 返回类型添加专门化只是将重复工作加倍。

无论如何要避免 void 返回类型的第二个专业化?

4

2 回答 2

4

您可以使用Return并专门operator()处理。无需复制整个模板。

// I think it's a shame if c++0x really gets rid of std::identity. It's soo useful!
template<typename> struct t2t { };

// Specialization for signatures with no parameters
template< typename Host, typename Return >
class Method< Host, Return () >
{
public:
  typedef Return (Host::*MethodType)();
  Method( Host * host, MethodType method ) : m_Host(host), m_Method(method) {}

  Value operator()() { return call(t2t<Return>()); }

private:
  Value call(t2t<void>) { return Value(); }

  template<typename T>
  Value call(t2t<T>) { return Value((m_Host->*m_Method)()); }

private:
  Host       * m_Host;
  MethodType   m_Method;
};
于 2010-09-11T11:20:18.253 回答
2

不,绝对没有办法通过void. 这是语言的不规范。

函数参数列表(void)被翻译为(). Bjarne 更喜欢后者而不是前者,并且不情愿地将 C 约定作为一种非常有限的语法糖。您甚至不能替换 的 typedef 别名void,当然也不能有任何其他参数。

我个人认为这是一个坏主意。如果你可以写void(expr),那么你应该能够“初始化”一个匿名参数类型void。如果您还可以编写具有任意数量void参数的函数,那么将有一种方法可以以未指定的顺序执行多个表达式,这将以某种方式表达并发性。

至于处理不同大小的参数列表(也称为可变参数),请在开始尝试学习 Boost Preprocessor 之前查看 C++0x 中的可变参数模板。

于 2010-09-06T03:48:00.810 回答