I have an almost working solution. However, it fails to compile some simple cases, and I can\'t decipher the error message.
My current solution:
#de
Thank you for a great question that caused me to look into a few new areas in C++11.
Anthony W. was faster with a solution; but I still want to share mine, which also uses a helper struct (and it's admittedly more verbose). First, let me share a link to a similar question trailing return type using decltype with a variadic template function; the answers there are based on the same idea of a helper struct, so their authors deserve credit.
The code below is checked with ideone. Note that it does not use AUTO_RETURN anymore, as the helper struct now takes care of the type.
template< typename BinaryFunc, typename First, typename... Types >
struct helper;
template< typename BinaryFunc, typename First>
struct helper {
typedef decltype(std::declval()) type;
};
template< typename BinaryFunc, typename First, typename Second >
struct helper {
typedef decltype(
std::declval()( std::declval(), std::declval() )
) type;
};
template< typename BinaryFunc, typename First, typename Second, typename... Rest >
struct helper {
typedef typename helper< BinaryFunc,
typename helper::type,
Rest...
>::type
type;
};
template< typename BinaryFunc, typename First, typename Second >
typename helper::type
foldl( BinaryFunc&& func, First&& first, Second&& second ) {
return func( std::forward(first), std::forward(second) );
}
template< typename BinaryFunc, typename First, typename Second, typename... Rest >
typename helper::type
foldl( BinaryFunc&& func, First&& first, Second&& second, Rest&&... rest ) {
return foldl(
std::forward(func),
func( std::forward(first), std::forward(second) ),
std::forward(rest)...
);
}