Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is T&& instantiated as int&?

Can anyone please explain why this compiles and why does t end up with type int&?

#include <utility>

void f(int& r)
{
    ++r;
}

template <typename Fun, typename T>
void g(Fun fun, T&& t) 
{ 
    fun(std::forward<T>(t)); 
}

int main()
{
    int i = 0;

    g(f, i);
}

I see this on GCC 4.5.0 20100604 and GDB 7.2-60.2

like image 913
Šimon Tóth Avatar asked Dec 01 '10 18:12

Šimon Tóth


People also ask

What is the T used for?

The t-test is a test used for hypothesis testing in statistics and uses the t-statistic, the t-distribution values, and the degrees of freedom to determine statistical significance.

Why is the T wave positive?

The T wave represents ventricular repolarization. Generally, the T wave exhibits a positive deflection. The reason for this is that the last cells to depolarize in the ventricles are the first to repolarize.

What is the T wave do?

The T wave on the ECG (T-ECG) represents repolarization of the ventricular myocardium. Its morphology and duration are commonly used to diagnose pathology and assess risk of life-threatening ventricular arrhythmias.

How do you calculate the t-value?

To find the t value: Subtract the null hypothesis mean from the sample mean value. Divide the difference by the standard deviation of the sample. Multiply the resultant with the square root of the sample size.


1 Answers

Because of perfect forwarding, when the argument to P&& is an lvalue, then P will be deduced to the argument's type plus having a & attached. So you get int & && with P being int&. If the argument is an rvalue then P will be deduced to only the argument's type, so you would get an int&& argument with P being int if you would pass, for example 0 directly.

int& && will collapse to int& (this is a semantic view - syntactically int& && is illegal. But saying U && when U is a template parameter or a typedef refering to a type int&, then U&& is still the type int& - i.e the two references "collapse" to one lvalue reference). That's why t has type int&.

like image 113
Johannes Schaub - litb Avatar answered Oct 02 '22 00:10

Johannes Schaub - litb