Modern Native C++ Development for Maximum Productivity

Sign in to queue

The Discussion

  • User profile image

    The type of prime *is* typeable. It is of type: std::function< bool(int) >

  • User profile image


    That's actually not quite right. A Lambda of the form [](int x) { return true; } is _assignable_ to std::function<bool(int)>, but that's not quite the same as saying the type of the lambda _is_ std::function<bool(int)>.

    bool function(int);

    struct functor
    bool operator()(int);

    std::function<bool(int)> f;

    // I can assign a function pointer to f, is f of type bool (*)(int)?
    f = &function;

    // I can assign a functor to f, is f of type functor?
    f = functor();

    // And now a lambda
    f = [](int x){ return false; };

    In the above example, f is none of a function pointer, a function object, or a lambda, yet it accepts all of them just fine.

    Lastly, consider:

    decltype([](int x) { return false; }) x = [](int x){ return false; };

    In the above, I am trying really really hard to get the correct type of the lambda so I can name the type explicitly, yet (in VS2010 anyway) that won't compile. So, it seems that indeed, the type of a lambda is untypable, even though that doesn't stop us from assigning it to std::function or doing other useful things with it.

Add Your 2 Cents