vendredi 23 janvier 2015

Distinguish between functor signatures with boost::result_of

I'd like to call different function overloads depending on the signature of the functor I pass in these function.


This code works fine with C++11. Is it possible to make it work with 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.

Aucun commentaire:

Enregistrer un commentaire