Ask*_*aga 6 c++ constants c++20 c++-modules
我一直在尝试新的模块功能,但无法导出全局常量。导出似乎编译得很好,但导入时编译器抱怨未声明常量。我的代码:
\n测试.cpp
\nexport module test;\n\nexport struct my_type { int x, y; };\nexport constexpr int my_constant = 42;\nexport int my_function() { return my_constant; }\nRun Code Online (Sandbox Code Playgroud)\n主程序
\nimport test;\n\nint main() {\n my_type t{1, 2};\n int i = my_function();\n int j = my_constant; // <- error here\n}\nRun Code Online (Sandbox Code Playgroud)\n我究竟做错了什么?我在 Linux 上使用 g++ 11.1.0:g++-11 -std=c++20 -fmodules-ts test.cpp main.cpp -o main
错误信息是:error: \xe2\x80\x98my_constant\xe2\x80\x99 was not declared in this scope
const 限定的变量默认具有内部链接,因此可能需要将其写为
export extern const int my_constant = 42;
Run Code Online (Sandbox Code Playgroud)
根据https://en.cppreference.com/w/cpp/language/storage_duration,定义export应该使变量具有外部链接,因此您可能已经遇到了 C++20 尚未完全实现的角落之一。
| 归档时间: |
|
| 查看次数: |
1733 次 |
| 最近记录: |