Lambda assignment for std :: function

Why is the second assignment allowed when the return type is std :: nullptr_t? With function pointers, this is forbidden.

And why does the second lambda not work?

#include <cstdio> #include <functional> int main() { std::function<void* ()> f; f = []() -> void* { printf ("runs\n"); return nullptr; }; f(); f = []() { printf ("doesn't run\n"); return nullptr; // -> std::nullptr_t }; f(); return 0; } 
+6
source share
1 answer

std::function allows you to store something if you provided the following for signature:

  • all argument types are implicitly converted to the argument types of the stored called object and
  • the return type of the stored called object is implicitly converted to the signature return type

std::nullptr_t implicitly converted to any type of pointer and gives the value of a null pointer of this type of pointer.

Note that your code is actually not valid C ++ 11, since in the second lambda you have not only return expr; , since such a conclusion will not be output. GCC (and Clang, IIRC) will implement this as an extension, as it will be part of the standard at some time.

+8
source

All Articles