The only possible return type return function in C ++ 11 is the return type lambda. However, C ++ 11 restricts the use of lambda. It works:
auto add = [](int a, int b) { return a + b; };
This is valid and defines add as a lambda that defines a member function operator() that returns an int . Since lambda doesn't capture anything, you can even write
auto add = +[](int a, int b) { return a + b; };
to make add regular function pointer: it gets the type int(*)(int, int) .
However, C ++ 11 does not allow parameter types to be specified as auto or to prevent add defined as a template variable, so you cannot use this to generally infer the type of the return value. An attempt to wrap it in a template class failed:
template <typename A, typename B> struct S { static auto add = [](A a, B b) { return a + b; }; };
It is not add to initialize add in a class, and you cannot use auto if a member is not initialized in the class. Also, even if it worked, it would not be possible to subtract A or B , which seems to be more than what you need.
Given these limitations, I see no alternative but to repeat the expression. However, you can hide the repetition in a trivial macro.
Or an option noted by Mark Gliss,
which looks a little cleaner.
There might be something like this in Boost, I don't know. Despite the triviality, Boost seems redundant here.