【发布时间】:2022-12-01 23:41:56
【问题描述】:
I have recently learnt about incomplete types and that under certain conditions they can be used as template arguments. In particular, like void, struct incomplete; are both incomplete types. Then I wrote the following program that works with gcc but not with msvc and clang. Live demo
struct incomplete;
template<typename T> struct C
{
static constexpr T t{};
};
template<class T>
struct myClass {
C<T> new_t() { return {}; }
};
int main() {
myClass<incomplete> d;
d.new_t();
}
As we can see the above program compiles with gcc but not with msvc and clang. So I want to know which is the correct technical behavior.
Clang says:
<source>:4:24: error: constexpr variable cannot have non-literal type 'const incomplete'
static constexpr T t{};
while msvc says:
<source>(4): error C2027: use of undefined type 'incomplete'
<source>(1): note: see declaration of 'incomplete'
while GCC accepts the code with both c++17 as well as c++20.
Which compiler is correct here?
【问题讨论】:
-
Interestingly, as soon as you odr-use
t, gcc also rejects the code. This might be an indication as to why gcc accepts it in the first place. -
Looks like ill-formed.
-
You need to "complete" the type eventually. Else the program is ill-formed.
标签: c++ language-lawyer c++20