Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++20

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

like image 890
Dmitry Avatar asked Nov 02 '25 13:11

Dmitry


1 Answers

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.

like image 188
Ted Lyngmo Avatar answered Nov 04 '25 05:11

Ted Lyngmo