How can Boost be used to achieve C++14-style auto return types?

The only possible deduced function return type in C++11 is the return type of a lambda. C++11 restricts the use of lambdas, though. This works:

auto add = [](int a, int b) { return a + b; };

This is valid, and defines add as a lambda that defines an operator() member function that returns int. Since the lambda doesn’t capture anything, you can even write

auto add = +[](int a, int b) { return a + b; };

to make add a regular pointer-to-function: it gets type int(*)(int, int).

However, C++11 doesn’t allow parameter types to be specified as auto, nor to let add be defined as a template variable, so you cannot use this to generically deduce a return type. An attempt to wrap it up in a template class fails:

template <typename A, typename B>
struct S { static auto add = [](A a, B b) { return a + b; }; }; // invalid

It is invalid to initialise add in-class here, and you cannot use auto unless the member is initialised in-class. Besides, even if it did work, it wouldn’t allow deduction of A or B, which seems to be more what you’re after.

Given those limitations, I don’t see any alternative but to repeat the expression. You could hide the repetition in a trivial macro, though.

#define AUTO_RETURN(func, ...) auto func -> decltype(__VA_ARGS__) { return __VA_ARGS__; }

template <typename A, typename B>
AUTO_RETURN(add(A a, B b), a + b)

Or the variant pointed out by Marc Glisse,

#define RETURNS(...) noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) { return __VA_ARGS__; }

template <typename A, typename B>
auto add(A a, B b) RETURNS(a + b)

which looks a bit cleaner.

There might be something like this in Boost already, I don’t know. Regardless, given the triviality, Boost seems overkill here.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)