C++ 内部类的奇怪constexpr行为
有人能解释一下吗C++ 内部类的奇怪constexpr行为,c++,c++11,constexpr,C++,C++11,Constexpr,有人能解释一下吗 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
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
};
模板
结构C
{
};
结构X
{
枚举E{A,B,C};
静态constexpr size_t noElems(){return C+1;};
};
结构K
{
C cx;//这不需要编译
};
结构
{
结构Z
{
枚举E{A,B,C};
静态constexpr size_t noElems(){return C+1;};
};
C cyz;//这不会编译
C cyz;//带有结构声明
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
};
在声明时不可用
C<Z> cyz2;
cyz2;
Clang抱怨未定义的函数'noElems'不能用于常量表达式
我想可能会有答案。你能在你的问题中添加编译错误吗?请参阅。+1回答不错。我想知道Z::C是如何可用的。我编辑了我的答案,我认为它是关于noElems的内联,这使得编译器在编译后要解析它cyz的声明,虽然enum E可用,这就是cyz1工作的原因
C<Z> cyz2;