Nic*_*las 5 c++ templates struct language-lawyer
我们有这样的代码:
template <typename T>
struct A {
static constexpr A a = A{};
};
template <typename T>
struct B {
T a;
};
B<A<int>> b;
Run Code Online (Sandbox Code Playgroud)
GCC 13 似乎对 L.3 感到满意,但对 clang 16 不满意。
<source>:3:21: error: constexpr variable cannot have non-literal type 'const A<int>'
static constexpr A a = A{};
^
<source>:9:4: note: in instantiation of template class 'A<int>' requested here
T a;
^
<source>:12:11: note: in instantiation of template class 'B<A<int>>' requested here
B<A<int>> b;
^
<source>:3:21: note: incomplete type 'const A<int>' is not a literal type
static constexpr A a = A{};
^
<source>:2:8: note: definition of 'A<int>' is not complete until the closing '}'
struct A {
^
1 error generated.
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/brYMf9har
哪个编译器是正确的?
叮当是正确的。
对象不应被定义为不完整的类型;[基本.def]/5。static constexpr 数据成员是隐式内联的,因此它的声明是一个定义。并且由于此定义发生在其自身的定义内部A,A因此在此上下文中仍然不完整,除非上下文是完整类上下文。静态数据成员的初始值设定项不是完整的类上下文;[class.mem.general]/7。
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |