#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
关于c++ - 为什么 std::is_copy_constructible 的行为不如预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54474692/