我有以下代码:
#include <iostream>
class A;
int main()
{
std::cout << std::is_constructible<A>::value << std::endl;
}
当我使用 GCC 8.3 时,此代码编译。但是,当我使用 Clang 8.0 ,我得到一个编译错误,不完整的类型不能用于类型特征。
哪一个是正确的?我是否可以在不完整的类型上使用 is_constructible
(预期值为 false
),还是不允许?
最佳答案
行为未定义。
[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的元功能。您的代码违反的契约(Contract)。 libc++ 很慷慨地通知您。
请注意,将前提条件放在那里并使其未定义是有原因的。一个模板的两个实例化点具有不同含义的程序是ill-formed NDR .唯一明智的做法是需求完整类型。毕竟,那是特质最有用的时候。
关于c++ - 不完整类型上的 std::is_constructible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55831521/