Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:38:03+00:00 2026-06-09T09:38:03+00:00

Let’s consider the following code snippet void Test() { int x = 0; int&

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:38:05+00:00Added an answer on June 9, 2026 at 9:38 am

    The simplest way to think about it is comparing it to template argument deduction.

    Given:

    template<typename T>
    void deduce(T) { }
    

    If you call:

    deduce(px);
    

    then the template argument T will be deduced as int* and if you call

    deduce(rx);
    

    then T will be deduced as int, not int&

    You get the same types deduced when using auto.

    One could draw an analogy from pointer types expecting that the deduced type of arx is int&

    You’d have to have a fairly confused model of the C++ language to make that analogy. Just because they are declared in syntactically similar ways, as Type@ with a type and a modifier doesn’t make them work the same way. A pointer is a value, an object, and it can be copied and have its value altered by assignment. A reference is not an object, it’s a reference to some object. A reference can’t be copied (copying it copies the referent) or altered (assigning to it alters the referent). A function that returns a pointer returns an object by value (the object in question being a pointer object), but a function that returns a reference (like your GetBigClass()) returns an object by reference. They’re completely different semantics, trying to draw analogies between pointers and references is doomed to failure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider the following scenario: I've a master table with a detail table. The
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and
Let's say we have this code: <form action='' method='POST' enctype='multipart/form-data'> <input type='file' name='userFile'><br> <input
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have the following classes : public class MyProductCode { private String
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.