Why does a class inherited from lambda type lack conversion to function pointer in Visual C++?

1 day ago 2
ARTICLE AD BOX

If a lambda is capture-less, its closure type has conversion operator to function pointer. For example,

auto l = []{}; using F = decltype(+l); //pointer to function type constexpr F p = l;

And if a class inherits closure type, it is reasonable to expect that conversion operator is inherited as well. But in MSVC compiler it is not, and one has to define it explicitly:

using L = decltype(l); struct A : L { //only required for MSVC: constexpr operator F() { return static_cast<L&>(*this); } }; constexpr F q = A{};

Without operator A::F() MSVC complains:

error C2440: 'initializing': cannot convert from 'A' to 'const F' note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Online demo.

I thought that it is somehow related to the presence of several conversion operators in MSVC for distinct calling conventions, as described here. But manually created class with similar conversions permits inheritance of the conversion operators, online demo.

Why the inheritance does not work for closure types then as expected?

Fedor's user avatar

3

Read Entire Article