[英]C++20 | std::is_constant_evaluated() and const variables
[英]undefined reference to `std::is_constant_evaluated()' in c++20 gcc
這只是一個最小的可重現示例:
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;
}
此代碼在使用g++ -std=c++20 -fmodules-ts test.cpp
時會產生此錯誤:
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
相反,如果我直接在哈希結構中定義模板參數,則沒有錯誤!
template<>
struct std::hash<my_class<char>> {
std::size_t operator()(my_class<char> const &s) const noexcept {
return 0;
}
};
我很困惑是什么錯誤!
我認為這只是@ildjarn 在評論中建議的 GCC 模塊實現中的一些錯誤。
通過在源代碼中添加這一行,我能夠擺脫錯誤:
constexpr bool std::is_constant_evaluated() noexcept;
但我不確定到底出了什么問題,或者為什么添加它可以解決錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.