[英]Temporary const array not binding to rvalue reference
I have the following test program: 我有以下测试程序:
#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));
}
I would expect that the line marked with an arrow would call the rvalue overload like the non-const call just above it but it doesn't. 我希望用箭头标记的行会调用rvalue重载,就像它上面的非const调用一样,但事实并非如此。
Is this just a bug in GCC or is there something in the standard that causes the lvalue overload to be selected? 这只是GCC中的一个错误,还是标准中有一些导致选择左值超载的东西?
According to the standard §12.2 [class.temporary]
: 根据标准
§12.2 [class.temporary]
:
Temporaries of class type are created in various contexts: binding a reference to a prvalue (8.5.3), returning a prvalue (6.6.3), a conversion that creates a prvalue (4.1, 5.2.9, 5.2.11, 5.4), throwing an exception (15.1), entering a handler (15.3), and in some initializations (8.5).
类型的临时数在各种上下文中创建:绑定对prvalue的引用(8.5.3),返回prvalue(6.6.3),创建prvalue的转换(4.1,5.2.9,5.2.11,5.4) ,抛出异常(15.1),进入处理程序(15.3),以及一些初始化(8.5)。
So id<int const[]>{1, 2, 3, 4, 5}
is a temporary and therefore is an prvalue §3.10 [basic.lval]
: 所以
id<int const[]>{1, 2, 3, 4, 5}
是临时的,因此是§3.10 [basic.lval]
:
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object.
rvalue(历史上,因为rvalues可能出现在赋值表达式的右侧)是一个xvalue,一个临时对象(12.2)或其子对象,或者一个与对象无关的值。
A prvalue (“pure” rvalue) is an rvalue that is not an xvalue.
prvalue(“纯”rvalue)是一个不是xvalue的rvalue。
Hence shall be selected overloaded function with rvalue reference argument. 因此应选择具有rvalue引用参数的重载函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.