Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rvalue reference parameters and template functions

If I define a function which accepts an rvalue reference parameter:

template <typename T>
void fooT(T &&x) {}

I can call it, using GCC 4.5, with either a, ar, or arr:

int a, &ar = a, &&arr = 7;
fooT(a); fooT(ar); fooT(arr);

However, calling a similar, non-template function,

void fooInt(int &&x) {}

with any of those three arguments will fail. I was preparing to strengthen my knowledge of forward, but this has knocked me off course. Perhaps it's GCC 4.5; I was surprised to find that the first example from A Brief Introduction to Rvalue References also gives a compile error:

A a;
A&& a_ref2 = a;  // an rvalue reference
like image 276
user2023370 Avatar asked Apr 03 '11 20:04

user2023370


1 Answers

The behavior of deduction in template parameters is unique, and is the reason your template version works. I've explained exactly how this deduction works here, in the context of another question.

Summarized: when the argument is an lvalue, T is deduced to T&, and T& && collapses to T&. And with the parameter at T&, it is perfectly valid to supply an lvalue T to it. Otherwise, T remains T, and the parameter is T&&, which accepts rvalues arguments.

Contrarily, int&& is always int&& (no template deduction rules to coerce it to something else), and can only bind to rvalues.

like image 131
GManNickG Avatar answered Sep 30 '22 23:09

GManNickG