Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of rvalue reference and auto

Tags:

c++

c++11

Given the code below, everything works. How come that the variable d is reference to int? What is going on?

int main()
{
    int a= 10;
    int &&b = a+10; // b is int &&
    auto c =b+10; // c is int
    auto &&d = a; // d is int&
    //int &&di = a; // error, as expected
    return (0);
}
like image 874
dodol Avatar asked Apr 19 '12 12:04

dodol


1 Answers

This has to do with the reference collapsing rules in type deduction.

A& & becomes A&
A& && becomes A&
A&& & becomes A&
A&& && becomes A&&
like image 64
bames53 Avatar answered Oct 14 '22 12:10

bames53