为什么`std :: is_constructible_v <int [2],int,=“”int =“”> == false`

时间:2017-11-28 07:44:56

标签: c++ arrays c++17

可重复的例子:

#include <type_traits>
static_assert(std::is_constructible_v<int[2], int, int>, "fails against my expectations");

我用clang 5和gcc 7进行了测试。

2 个答案:

答案 0 :(得分:7)

来自ref

  

如果T是对象或引用类型且变量定义T obj(std::declval<Args>()...); 格式正确,则提供成员常量值等于true。在所有其他情况中,值为false。

在您的示例中,T obj(std::declval<Args>()...); 格式正确。

这是因为int[2]是一个普通的数组,它没有任何构造函数。

结果,这个:

int obj[2](int, int);

是不正确的。

数组是aggregates,因此聚合初始化在这里起作用,而不是构造。

答案 1 :(得分:2)

std::is_constructible的目的和定义是检查指定类型的对象是否可以如下构造:

T obj(std::declval<Args>()...);

以上对阵列来说根本不是很好。数组没有任何构造函数,它是一个聚合,应该使用聚合初始化进行初始化。