`auto` specifier type deduction for references

Viewed 3298

Let's consider the following code snippet

void Test()
  {
  int x = 0;

  int& rx = x;
  int* px = &x;

  auto apx = px;    // deduced type is int*
  auto arx = rx;    // deduced type is int
  }

One could draw an analogy from pointer types expecting that the deduced type of arx is int&, but it is int in fact.

What is the rule in Standard which governs that? What is the reason behind it? Sometimes I get caught by it in a case like this:

const BigClass& GetBigClass();
...
auto ref_bigclass = GetBigClass();   // unexpected copy is performed
2 Answers
Related