为什么 std::is_copy_constructible<T&&>::value return 为假?
Why does std::is_copy_constructible<T&&>::value return false?
似乎 is_copy_constructible<T&&>
是 false
,即使 is_copy_constructible<T>
是 true
相同类型 T
。我用 gcc 和 clang 测试了这个,得到了相同的结果。这是预期的行为吗?标准在哪里定义这个?这种行为的原因是什么?
编译错误的示例代码"int&& doesn't work"
:
#include <type_traits>
int main() {
static_assert(std::is_copy_constructible<int>::value, "int doesn't work");
static_assert(std::is_copy_constructible<int&>::value, "int& doesn't work");
static_assert(std::is_copy_constructible<int&&>::value, "int&& doesn't work");
return 0;
}
我在尝试对模板参数创建约束以确保模板仅适用于可复制构造类型时遇到了这种情况。我是否需要为右值引用类型创建一个特例,以便让我的模板使用右值引用来复制可构造类型?
is_copy_constructible<T>
定义为 is_constructible<T, const T&>
。对于T = int &&
,后者变成了is_constructible<int&&, int&>
,这显然是错误的:
int & f();
int && r(f()); // ill-formed
似乎 is_copy_constructible<T&&>
是 false
,即使 is_copy_constructible<T>
是 true
相同类型 T
。我用 gcc 和 clang 测试了这个,得到了相同的结果。这是预期的行为吗?标准在哪里定义这个?这种行为的原因是什么?
编译错误的示例代码"int&& doesn't work"
:
#include <type_traits>
int main() {
static_assert(std::is_copy_constructible<int>::value, "int doesn't work");
static_assert(std::is_copy_constructible<int&>::value, "int& doesn't work");
static_assert(std::is_copy_constructible<int&&>::value, "int&& doesn't work");
return 0;
}
我在尝试对模板参数创建约束以确保模板仅适用于可复制构造类型时遇到了这种情况。我是否需要为右值引用类型创建一个特例,以便让我的模板使用右值引用来复制可构造类型?
is_copy_constructible<T>
定义为 is_constructible<T, const T&>
。对于T = int &&
,后者变成了is_constructible<int&&, int&>
,这显然是错误的:
int & f();
int && r(f()); // ill-formed