undefined reference to `std::is_constant_evaluated()' in c++20 gcc

243 Views Asked by At

This is just a minimal reproducible example:

import <unordered_set>;
import <functional>;

template<class c>
class my_class {
};

template<class c>
struct std::hash<my_class<c>> {
    std::size_t operator()(my_class<c> const &s) const noexcept {
        return 0;
    }
};


int main() {
    std::unordered_set<my_class<char>> x;
}

This code, when compiled with g++ -std=c++20 -fmodules-ts test.cpp produces this error:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\SOURAV~1\AppData\Local\Temp\cclOkCUA.o:test.cpp:(.text$_ZNSt16allocator_traitsISaIPNSt8__detail15_Hash_node_baseEEE10deallocateERS3_PS2_y[_ZNSt16allocator_traitsISaIPNSt8__detail15_Hash_node_baseEEE10deallocateERS3_PS2_y]+0x2d): undefined reference to `std::is_constant_evaluated()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\SOURAV~1\AppData\Local\Temp\cclOkCUA.o:test.cpp:(.text$_ZNSt16allocator_traitsISaINSt8__detail10_Hash_nodeI8my_classIcELb0EEEEE10deallocateERS5_PS4_y[_ZNSt16allocator_traitsISaINSt8__detail10_Hash_nodeI8my_classIcELb0EEEEE10deallocateERS5_PS4_y]+0x2d): undefined reference to `std::is_constant_evaluated()'
collect2.exe: error: ld returned 1 exit status

Instead if I define template argument directly in hash struct, there is no error!

template<>
struct std::hash<my_class<char>> {
    std::size_t operator()(my_class<char> const &s) const noexcept {
        return 0;
    }
};

I am confused what is the error!

1

There are 1 best solutions below

0
On

I think it is just some bug in GCC modules implementation as suggested by @ildjarn in comments.

I was able to get rid of error by adding this line in the source code:
constexpr bool std::is_constant_evaluated() noexcept;

But I'm not sure what exactly was wrong or why did adding this solved the error.