C++ - 无法使用嵌套类的 constexpr 函数

问题描述 投票:0回答:1
struct Foo
{
    struct Bar
    {
        int data = 0;

        //constexpr Bar() = default; // Doesn't work either
        constexpr Bar() : data(0) {}
    };

    static constexpr Bar bar = {}; // ERROR
    //static constexpr Bar bar = {0}; // Works if the ctor from Bar is removed
};

Clang 和 GCC(使用 std=c++20)说我尝试使用的构造函数未定义。但如果我将

constexpr
更改为
inline
,它就会起作用。我想了解使用“contexpr”有什么问题。

c++ c++20 constexpr
1个回答
0
投票

您看到的错误与

static constexpr Bar bar
内的
struct Foo
成员变量的初始化有关。当您定义
constexpr
变量时,初始化器必须是
constant expression
,这是比简单的
compile-time constant
更严格的要求。

在您的情况下,

Bar
的默认构造函数使用成员初始化语法将
data
初始化为
0
。虽然这看起来像一个常量表达式,但实际上它不符合 C++ 标准对常量表达式的要求。

要初始化

constexpr
变量,用于初始化它的表达式必须是常量表达式。让代码与
constexpr
配合使用的一种方法是将
Bar
的默认构造函数更改为
constexpr constructor
,如下所示:

struct Bar
{
    int data = 0;
   
    constexpr Bar() = default;
};

通过此更改,您应该能够将

bar
定义为
constexpr
变量:

static constexpr Bar bar = {};

请注意,只有当您使用支持非聚合类的

constexpr
构造函数(C++17 中引入)的编译器时,此更改才有效。如果您使用的是旧版本的 C++ 标准,则需要坚持使用
inline
关键字而不是
constexpr

© www.soinside.com 2019 - 2024. All rights reserved.