不完整类型上的std :: is_constructible [英] std::is_constructible on incomplete types
问题描述
我有以下代码:
#include <iostream>
class A;
int main()
{
std::cout << std::is_constructible<A>::value << std::endl;
}
当我使用 GCC 8.3 ,此代码会编译。但是,当我使用 Clang 8.0 时,出现编译错误,即不能在类型中使用不完整的类型特质。
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.
哪个是正确的?我可以在不完整的类型上使用 is_constructible
(预期值为 false
),还是被禁止?
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?
推荐答案
行为未定义。
[meta.unary.prop]
template <class T, class... Args> struct is_constructible;
T
和参数包中的所有类型 Args
应该是完整类型,
(可能是经过cv限定)是无效类型,或者是未知范围的数组。
T
and all types in the parameter pack Args
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.
请记住,把那个前提放在那儿然后让它保持不确定状态是为了原因。模板的两个实例化点具有不同含义的程序是格式错误的NDR 。唯一明智的做法是需求完整类型。毕竟,无论如何,这就是特质最有用的时候。
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.
这篇关于不完整类型上的std :: is_constructible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!