为什么 std::is_copy_constructible<T&&>::value 返回 false?

Why does std::is_copy_constructible<T&&>::value return false?

本文关键字:gt value false 返回 lt is copy constructible 为什么 std      更新时间:2023-10-16

对于相同类型的T,即使is_copy_constructible<T>trueis_copy_constructible<T&&>似乎也是false。我已经用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;
}

我在尝试对模板参数创建约束以确保模板仅适用于可复制构造类型时遇到了这种情况。我是否需要为r值引用类型创建一个特例,以便让我的模板使用r值引用来复制可构造类型?

is_copy_constructible<T>定义为is_constructible<T, const T&>。对于T = int &&,后者变为is_constructible<int&&, int&>,这显然是错误的:

int & f();
int && r(f());   // ill-formed