Sim*_*ple 10 c++ arrays const rvalue-reference c++11
我有以下测试程序:
#include <iostream>
#include <type_traits>
#include <utility>
template<typename Ty, std::size_t N>
void foo(Ty (&&)[N])
{
std::cout << "Ty (&&)[" << N << "]\t" << std::is_const<Ty>::value << '\n';
}
template<typename Ty, std::size_t N>
void foo(Ty (&)[N])
{
std::cout << "Ty (&)[" << N << "]\t" << std::is_const<Ty>::value << '\n';
}
template<typename Ty>
using id = Ty;
int main()
{
std::cout.setf(std::cout.boolalpha);
foo(id<int[]>{1, 2, 3, 4, 5});
foo(id<int const[]>{1, 2, 3, 4, 5}); // <-- HERE.
int xs[]{1, 2, 3, 4, 5};
foo(xs);
int const ys[]{1, 2, 3, 4, 5};
foo(ys);
foo(std::move(xs));
foo(std::move(ys));
}
Run Code Online (Sandbox Code Playgroud)
我希望用箭头标记的行会调用rvalue重载,就像它上面的非const调用一样,但事实并非如此.
这只是GCC中的一个错误,还是标准中有一些导致选择左值超载的东西?
根据标准\xc2\xa712.2 [class.temporary]:
\n\n\n类类型的临时变量在各种上下文中创建:将引用绑定到纯右值 (8.5.3)、返回纯右值 (6.6.3)、创建纯右值的转换 (4.1、5.2.9、5.2.1)。 11、5.4),抛出异常(15.1),进入处理程序(15.3),以及一些初始化(8.5)。
\n
id<int const[]>{1, 2, 3, 4, 5}临时值也是如此,纯右值也是如此\xc2\xa73.10 [basic.lval]:
\n\n\n右值(历史上如此称呼,因为右值可能出现在赋值表达式的右侧)是 xvalue、临时对象 (12.2) 或其子对象,或者不关联的值与一个物体。
\n\n右值 (\xe2\x80\x9cpure\xe2\x80\x9d 右值) 是不是 x 值的右值。
\n
因此应选择带有右值引用参数的重载函数。
\n