用boost :: result_of区分仿函数签名

时间:2015-01-23 16:34:58

标签: c++ c++11 boost

我想调用不同的函数重载,具体取决于我在这些函数中传递的仿函数的签名。

此代码适用于C ++ 11。 是否可以使它适用于C ++ 03?

#include <boost/utility/result_of.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
using namespace boost;

// (1) 
template< class F >
typename enable_if<is_void<typename result_of<F(int)>::type>, bool>::type
f (F const& x)
{
  x(1);
  return true;
}

// (2)
template< class F >
typename enable_if<is_void<typename result_of<F(int,int)>::type>, bool>::type
f (F const& x)
{
  x(1,2);
  return true;
}

struct A {
  typedef void result_type;
  void operator()(int) const { }
};

int main()
{
  A a;
  f (a); // expecting f(F) version (1) to be called here.
}

clang ++ -std = c ++ 03 -c r.cc

r.cc:30:3: error: call to 'f' is ambiguous
  f (a);
  ^
r.cc:8:1: note: candidate function [with F = A]
f (F const& x)
^
r.cc:16:1: note: candidate function [with F = A]
f (F const& x)
^
1 error generated.

0 个答案:

没有答案