Passing an Lvalue to a parameter of RValue

做~自己de王妃 提交于 2019-12-10 16:53:14

问题


I wanted to know how this is possible ?

template<typename T>
void Test(T&& arg)
{
    arg = 14;
}


int a = 23;
Test(a);

My question is that the function Test requires an argument of type Rvalue however it seems to also accept parameter of type lvalue. Why is that ? Is that because of presence of templates ? Because If i do something like this

void AnotherTest(int&& arg)
{
    arg = 14;
}

Then the function requires the parameter to be of type Rvalue. I would appreciate it if someone could explain why presence of Templates changes the behavior.


回答1:


The key, as you correctly imagined, is that it is a template and that the argument type is being deduced. When you call Test with an lvalue, the rules for argument type deduction when the argument is an rvalue-reference will deduce the type T to be an lvalue-reference, and thus the specialization becomes:

template <>
void Test<int&>(int & && arg)

At this point the reference collapsing rules kick in and the type of the argument becomes:

template <>
void Test<int&>(int & arg)

While the template takes an rvalue-reference, if the type is an lvalue-reference the argument becomes an lvalue-reference itself.



来源:https://stackoverflow.com/questions/23598692/passing-an-lvalue-to-a-parameter-of-rvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!