Search code examples
c++c++20

Why does GCC generate different types for the same lambda in a template context?


I've been playing around with stateful metaprogramming for a while.

I have discovered a weird behaviour in GCC

#include <type_traits>

//removing template will make it compile
template <typename = void>
void test() {
    // Replacing lambda with any other type will make it compile as well
    using NonDependentStaticType = decltype([](){});
    static_assert(std::is_same_v<NonDependentStaticType, NonDependentStaticType>);
}

int main() {
    test();
}

This code compiles for both latest clang and MSVC, but fails for GCC.

Am I correct that regardless of how compilers handle a default lambda template parameters the static_assert must not fail?

live example

UPDATE: Thanks to @Jarod42 for simplified code example


Solution

  • Am I correct that regardless of how compilers handle a default lambda template parameters the static_assert must not fail?

    Yes. It was a bug in gcc (for which I wrote 116714) and it has now been fixed for gcc 15 as you can verify yourself by rerunning your live example which uses gcc trunk.