gpt4 book ai didi

c++ - 内部类的奇怪 constexpr 行为

转载 作者:可可西里 更新时间:2023-11-01 16:15:05 24 4
gpt4 key购买 nike

谁能解释一下?

template<typename T, size_t S = T::noElems()>
struct C
{
};

struct X
{
enum E { A, B, C };
static constexpr size_t noElems() { return C+1; };
};

struct K
{
C<X> cx; // this DOES compile
};

struct Y
{
struct Z
{
enum E { A, B, C };
static constexpr size_t noElems() { return C+1; };
};
C<Z, Z::C+1> cyz; // this DOES compile

C<Z> cyz; // <--- this does NOT compile
};

最佳答案

结构体的声明

struct Y
{
struct Z
{
enum E { A, B, C };
static constexpr size_t noElems() { return C+1; };
};
C<Z, Z::C+1> cyz1; // this DOES compile

C<Z> cyz2; // <--- this does NOT compile
};

实体cyz1cyz2Z::noElems()的内联声明之前被解析,所以

的定义
static constexpr size_t noElems() { return C+1; };

在声明时不可用

C<Z> cyz2;

关于c++ - 内部类的奇怪 constexpr 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25887516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com