vendredi 29 mars 2019

lambda with conversion function to pointer to function with c++ linkage

C++ standard has a below statement:

The closure type for a non-generic lambda-expression with no lambda-capture whose constraints (if any) are satisfied has a conversion function to pointer to function with C++ language linkage (10.5) having the same parameter and return types as the closure type’s function call operator.

To understand the statement much better, I used the cppinsights to see what the clang compiler says for the below function.

#include <iostream>

using test = void (*)(int);

int main()
{
    test t = [](int arg) { std::cout << arg << std::endl; };
}

cppinsights translates the function as:

#include <iostream>

using test = void (*)(int);

int main()
{

  class __lambda_7_11
  {
    public: inline void operator()(int arg) const
    {
      std::cout.operator<<(arg).operator<<(std::endl);
    }

    public: using retType_7_11 = void (*)(int);
    inline operator retType_7_11 () const
    {
      return __invoke;
    }

    private: static inline void __invoke(int arg)
    {
      std::cout.operator<<(arg).operator<<(std::endl);
    }


  } __lambda_7_11{};

  using FuncPtr_7 = test;
  FuncPtr_7 t = static_cast<void (*)(int)>(__lambda_7_11.operator __lambda_7_11::retType_7_11());
}

As usual, the compiler generates an anonymous class with operator() overloaded along with "conversion function to pointer to function" as specified by the standard.

What I don't understand is that why a "static __invoke" function is generated and the "conversion function" is internally calling "__invoke" (directly as a function pointer) without any parameter which is expected by "__invoke"?

Aucun commentaire:

Enregistrer un commentaire