问题
I have the following code:
#include <iostream>
class A;
int main()
{
std::cout << std::is_constructible<A>::value << std::endl;
}
When I use GCC 8.3, this code compiles. However, when I use Clang 8.0, I get a compilation error that incomplete types cannot be used in type traits.
Which one is correct? Am I allowed to use is_constructible
on an incomplete type (with an expected value of false
), or am I not allowed to?
回答1:
The behavior is undefined.
[meta.unary.prop]
template <class T, class... Args> struct is_constructible;
T
and all types in the parameter packArgs
shall be complete types, (possibly cv-qualified) void, or arrays of unknown bound.
That's a precondition of the meta-function. A contract that your code violates. libc++ is being generous by notifying you.
Mind you, that putting that precondition there and leaving it undefined otherwise is for a reason. A program where two points of instantiation of a template have different meanings is ill-formed NDR. The only sane course of action is demand complete types. And after all, that's when the trait is most useful anyway.
回答2:
Your code causes undefined behavior.
Cppreference states:
template< class T, class... Args > struct is_constructible;
T and all types in the parameter pack Args shall each be a complete type, (possibly cv-qualified) void, or an array of unknown bound. Otherwise, the behavior is undefined.
回答3:
Your code has undefined behavior. Per [meta.unary.prop] table 47 std::is_constructible
requires
T
and all types in the template parameter packArgs
shall be complete types, cvvoid
, or arrays of unknown bound.
emphasis mine
来源:https://stackoverflow.com/questions/55831521/stdis-constructible-on-incomplete-types