为什么 std::is_copy_constructible 没有按预期运行?
Why does std::is_copy_constructible not behave as expected?
#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:
If T is an object or reference type and the variable definition T
obj(std::declval()...); is well-formed, provides the member
constant value equal to true. In all other cases, value is false.
std::is_copy_constructible_v<int&>
应该给出与 std::is_constructible_v<int&, const int&>
相同的结果;然而,clang 7.0
给出了不同的结果,如上所示。
这种行为是否符合 C++ 标准?
is_copy_constructible 的参考说明是:
If T is not a referenceable type (i.e., possibly cv-qualified void or a function type with a cv-qualifier-seq or a ref-qualifier), provides a member constant value equal to false. Otherwise, provides a member constant value equal to 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
#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:
If T is an object or reference type and the variable definition T obj(std::declval()...); is well-formed, provides the member constant value equal to true. In all other cases, value is false.
std::is_copy_constructible_v<int&>
应该给出与 std::is_constructible_v<int&, const int&>
相同的结果;然而,clang 7.0
给出了不同的结果,如上所示。
这种行为是否符合 C++ 标准?
is_copy_constructible 的参考说明是:
If T is not a referenceable type (i.e., possibly cv-qualified void or a function type with a cv-qualifier-seq or a ref-qualifier), provides a member constant value equal to false. Otherwise, provides a member constant value equal to
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