I have some function objects, each of which defines different two-parameter operator overloads ():
class Foo {};
class Bar {};
struct Functor1 {
double operator()(const Foo &, const Bar &) { return 2.2; }
double operator()(const Bar &, const Foo &) { return 3.1; }
};
struct Functor2 {
int operator()(const Foo &, const Foo &) { return 2; }
int operator()(const Bar &, const Bar &) { return 3; }
};
Now what I want to do is write a utility that allows you to bind these functors one by one to the binary operator. Here is my attempt:
#define ATTACH_FUNCTOR_TO_OPERATOR(OPERATOR, FUNCTOR) \
template<typename Operand1, typename Operand2> \
decltype(FUNCTOR(std::declval<Operand1>(), std::declval<Operand2>())) \
operator OPERATOR(const Operand1 &operand1, const Operand2 &operand2) { \
return FUNCTOR(operand1, operand2); \
}
This works fine the first time:
ATTACH_FUNCTOR_TO_OPERATOR(+, Functor1());
but when I add a second call with the same statement:
ATTACH_FUNCTOR_TO_OPERATOR(+, Functor2());
then Visual Studio 2013 tells me that "the function template is already defined."
Note that for any pair of operands there is only one instance +, the return type decltype(FUNCTOR(std::declval<Operand1>(), std::declval<Operand2>()))can be allowed for any particular type. I expected that any other attempts to create instances would fail and not cause any problems - aka SFINAE.
enable_if .
++ 11?
, VS2013?