首页 > 解决方案 > 为什么 std::is_copy_constructible 的行为不符合预期?

问题描述

#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 是对象或引用类型且变量定义 T obj(std::declval()...); 是格式良好的,提供等于 true 的成员常量值。在所有其他情况下,value 为 false。

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

这种行为是否符合 C++ 标准?

标签: c++c++11standardstypetraitscompile-time-constant

解决方案


is_copy_constructible状态的参考是:

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

所以,这里is_copy_constructible<T>::value是一样的std::is_constructible<T, const T&>::value

所以在你的情况下:

std::is_constructible<int, const int&>::value将与 相同std::is_copy_constructible_v<int>

DEMO


推荐阅读