具有命名空间的 C++20 模块

发布于 2025-01-14 08:35:32 字数 1870 浏览 2 评论 0原文

我希望收到有关我在尝试学习 c++20 模块时遇到的问题的一些意见。
简而言之,我希望在模块中实现包含 const 和/或 constexpr 变量的命名空间,并将该模块导入到任何适用的实现文件中。 它可以很好地处理非 const / 非 constexpr 变量,但这并不理想;我想坚持使用 const 和/或 constexpr,具体取决于命名空间内的数据类型。

请参见下面的示例:

// test_module.cpp
module;
#include <string_view>

export module test_module;

export namespace some_useful_ns {
        /* produces a compile error when attempting to compile main.cpp */
        constexpr std::string_view str_view{"a constexpr string view"};
        const char* const chr{"a const char* const"};
        
        /* compiles just fine, but not ideal; prefer const / constexpr
        std::string_view str_view{"a non constexpr str view"};
        const char* chr{"a const char*"};
        */
}
// main.cpp
#include <iostream>

import test_module;

int main() {
        std::cout << some_useful_ns::str_view << std::endl;
        std::cout << some_useful_ns::chr << std::endl;
        return 0;
}

如下所示编译两个文件:

g++ -c --std=c++2a -fmodules-ts test_module.cpp
g++ -c --std=c++2a -fmodules-ts main.cpp
g++ main.o test_module.o

在链接 .o 文件之前,在像上面那样编译 main.cpp 时收到以下错误:

main.cpp: In function ‘int main()’:
main.cpp:6:38: error: ‘str_view’ is not a member of ‘some_useful_ns’
    6 |         std::cout << some_useful_ns::str_view << std::endl;
      |                                      ^~~~~~~~
main.cpp:7:38: error: ‘chr’ is not a member of ‘some_useful_ns’
    7 |         std::cout << some_useful_ns::chr << std::endl;
      |

我觉得这很奇怪,因为当我使用像这样的非 const / 非 constexpr 时它工作正常test_module.cpp 中注释掉的行。 此外,在使用不带模块的传统实现时,使用 const / constexpr 的工作方式与预期一样。

任何人都知道为什么我无法使用 const / constexpr 成功使用它?
如果有帮助,我正在使用 gcc (GCC) 11.2.1 20220127

提前致谢。

I am hoping to receive some input on an issue I encountered while attempting to learn c++20 modules.
In short, I would like a namespace containing const and/or constexpr variables to be implemented within a module, and import that module into any implementation files applicable.
It works fine with non const / non constexpr variables, however this isn't ideal; I'd like to stick with const and/or constexpr depending on the data types within the namespace.

Please see example below:

// test_module.cpp
module;
#include <string_view>

export module test_module;

export namespace some_useful_ns {
        /* produces a compile error when attempting to compile main.cpp */
        constexpr std::string_view str_view{"a constexpr string view"};
        const char* const chr{"a const char* const"};
        
        /* compiles just fine, but not ideal; prefer const / constexpr
        std::string_view str_view{"a non constexpr str view"};
        const char* chr{"a const char*"};
        */
}
// main.cpp
#include <iostream>

import test_module;

int main() {
        std::cout << some_useful_ns::str_view << std::endl;
        std::cout << some_useful_ns::chr << std::endl;
        return 0;
}

Compile the two files like below:

g++ -c --std=c++2a -fmodules-ts test_module.cpp
g++ -c --std=c++2a -fmodules-ts main.cpp
g++ main.o test_module.o

Before linking the .o files, I receive the following error when compiling main.cpp like above:

main.cpp: In function ‘int main()’:
main.cpp:6:38: error: ‘str_view’ is not a member of ‘some_useful_ns’
    6 |         std::cout << some_useful_ns::str_view << std::endl;
      |                                      ^~~~~~~~
main.cpp:7:38: error: ‘chr’ is not a member of ‘some_useful_ns’
    7 |         std::cout << some_useful_ns::chr << std::endl;
      |

I find this strange since it works fine when I use a non const / non constexpr like the lines commented out in test_module.cpp.
Also, using the const / constexpr works just as expected when using a traditional implementation without modules.

Anyone have a clue as to why I am unable to get this to work successfully with const / constexpr?
In case it helps, I am using gcc (GCC) 11.2.1 20220127

Thanks in advance.

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

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

发布评论

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

评论(1

策马西风 2025-01-21 08:35:32

这看起来像是一个编译器错误。

通常,const 限定的变量具有内部链接,并且无法导出具有内部链接的名称。然而......对于导出的此类变量有一个特定的限制。也就是说,导出的 const 限定变量没有内部链接。

This seems like a compiler bug.

Normally, const-qualified variables have internal linkage, and names with internal linkage cannot be exported. However... there's a specific carveout for such variables which are exported. That is, exported const-qualified variables don't have internal linkage.

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