David
David

Reputation: 28178

What's wrong with this result_of usage?

auto lambda = [](){ return 7; };
std::result_of<decltype(lambda)()>::type val = lambda(); //  'val' : illegal use of type 'void'

I get the error: 'val' : illegal use of type 'void'. Why would the type resolve as void?

I may be mistaken about what result_of gets me. I just want the return value from anything I can pass a std::function.

Upvotes: 4

Views: 1060

Answers (1)

mfontanini
mfontanini

Reputation: 21910

If your compiler fails to compile that, then don't use std::result_of:

decltype(lambda()) val = lambda();

That is exactly the same, and it should(well, could) work in VC2010.

You could also use auto, though I don't think this is what you want:

auto val = lambda();

Edit: Since you're using this in the return value of a function, then the decltype solution shown above works fine:

#include <type_traits>
#include <iomanip>
#include <iostream>

template<class Functor>
auto foo(const Functor &f) -> decltype(f()) {
    return f();
}

int main() {
    auto lambda = [](){ return 7; };
    auto val = foo(lambda);
    std::cout << std::boolalpha;
    std::cout << std::is_same<decltype(val), int>::value << std::endl;
}

Demo here.

Upvotes: 5

Related Questions