为什么std::is_copy_constructible的行为不符合预期

Why does std::is_copy_constructible not behave as expected?

本文关键字:不符合 constructible std is copy 为什么      更新时间:2023-10-16
#include <type_traits>
int main()
{
std::is_constructible_v<int&, const int&>; // false, as expected.
std::is_copy_constructible_v<int&>; // true, NOT as expected!
}

根据cppref:

如果T是对象或引用类型,并且变量定义Tobj(std::declval()…);成型良好,提供构件等于true的常数值。在所有其他情况下,value都是false。

std::is_copy_constructible_v<int&>应该给出与std::is_constructible_v<int&, const int&>相同的结果;然而,clang 7.0给出了如上所示的不同结果。

此行为是否符合C++标准

is_copy_constructible状态的引用是什么:

如果T不是可引用类型(即,可能是cv限定的void或具有cv限定符seq或ref限定符的函数类型),则提供等于false的成员常数值。否则,提供一个等于std::is_constructible<T, const T&>::value的成员常数值。

因此,这里的is_copy_constructible<T>::valuestd::is_constructible<T, const T&>::value相同。

所以在你的情况下:

CCD_ 7将与CCD_ 8相同。

参见DEMO