对`sTD :: IS_CONSTANT_EVALED()的未定义引用()在C++ 20 GCC中

发布于 2025-02-01 10:41:51 字数 1615 浏览 2 评论 0原文

这只是一个最小可重复的示例:

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

相反,如果我直接在Hash Struct中定义模板参数,则 此代码。 ,没有错误!

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

我很困惑什么是错误!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么敢 2025-02-08 10:41:51

我认为这只是@ildjarn在评论中建议的GCC模块实现中的一些错误。

我能够通过在源代码中添加此行来摆脱错误:
constexpr bool std :: is_constant_evaLED()noexcept;

,但我不确定到底出了什么问题,或者为什么添加此问题解决了错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文