basically how to make following code compile?
I know it failed because compiler was trying to evaluate something like ([](int &i){})(0)
but how to s
Two ways. First:
using TResult = decltype(f(_e));
or second:
using TResult = typename std::result_of<Lambda&(TElement&)>::type;
Your code implicity says that the TElement is a temprary/rvalue. The &
above makes them lvalues.
You might resolve it with:
template <typename Lambda, typename T>
struct lambda_return_type {
private:
template<typename U>
static constexpr auto check(U*) -> decltype(std::declval<Lambda>()(std::declval<U>()));
template<typename U>
static constexpr auto check(...) -> decltype(std::declval<Lambda>()(std::declval<U&>()));
public:
typedef decltype(check<T>(nullptr)) type;
};
and
void bar(Lambda f) {
typedef typename lambda_return_type<Lambda, TElement>::type TResult;
}
You may use following traits:
template <typename T>
struct return_type : return_type<decltype(&T::operator())>
{};
// For generic types, directly use the result of the signature of its 'operator()'
template <typename ClassType, typename ReturnType, typename... Args>
struct return_type<ReturnType(ClassType::*)(Args...) const>
{
using type = ReturnType;
};