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 6337863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:20:00+00:00 2026-05-24T19:20:00+00:00

Please take a look at the following simple code: class Foo { public: Foo(){}

  • 0

Please take a look at the following simple code:

class Foo
{
public:
  Foo(){}
  ~Foo(){}

  Foo(const Foo&){}
  Foo& operator=(const Foo&) { return *this; }
};

static Foo g_temp;
const Foo& GetFoo() { return g_temp; }

I tried to use auto like this:

auto my_foo = GetFoo();

I expected that my_foo will be a constant reference to Foo, which is the return type of the function. However, the type of auto is Foo, not the reference. Furthermore, my_foo is created by copying g_temp. This behavior isn’t that obvious to me.

In order to get the reference to Foo, I needed to write like this:

const auto& my_foo2 = GetFoo();
      auto& my_foo3 = GetFoo();

Question: Why does auto deduce the return type of GetFoo as an object, not a reference?

  • 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-05-24T19:20:01+00:00Added an answer on May 24, 2026 at 7:20 pm

    Read this article: Appearing and Disappearing consts in C++


    Type deduction for auto variables in C++0x is essentially the same as
    for template parameters. (As far as I know, the only difference
    between the two is that the type of auto variables may be deduced from
    initializer lists, while the types of template parameters may not be.)
    Each of the following declarations therefore declare variables of type
    int (never const int):

    auto a1 = i;
    auto a2 = ci;
    auto a3 = *pci;
    auto a4 = pcs->i;
    

    During type deduction for template parameters and auto variables, only
    top-level consts are removed. Given a function template taking a
    pointer or reference parameter, the constness of whatever is pointed
    or referred to is retained:

    template<typename T>
    void f(T& p);
    
    int i;
    const int ci = 0;
    const int *pci = &i;
    
    f(i);               // as before, calls f<int>, i.e., T is int
    f(ci);              // now calls f<const int>, i.e., T is const int
    f(*pci);            // also calls f<const int>, i.e., T is const int
    

    This behavior is old news, applying as it does to both C++98 and
    C++03. The corresponding behavior for auto variables is, of course,
    new to C++0x:

    auto& a1 = i;       // a1 is of type int&
    auto& a2 = ci;      // a2 is of type const int&
    auto& a3 = *pci;    // a3 is also of type const int&
    auto& a4 = pcs->i;  // a4 is of type const int&, too
    

    Since you can retain the cv-qualifier if the type is a reference or pointer, you can do:

    auto& my_foo2 = GetFoo();
    

    Instead of having to specify it as const (same goes for volatile).

    Edit: As for why auto deduces the return type of GetFoo() as a value instead of a reference (which was your main question, sorry), consider this:

    const Foo my_foo = GetFoo();
    

    The above will create a copy, since my_foo is a value. If auto were to return an lvalue reference, the above wouldn’t be possible.

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

Sidebar

Related Questions

Please take a look at this code: class Foo { public $barInstance; public function
Please take a look at the following code: public static void main(String[] args) {
Please take a look at this code: template<class T> class A { class base
Please take a look at the following code: Public Sub Method(Of TEntity As EntityObject)(ByVal
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
Would someone please take a look at the following code fragments (all from one
Please take a look at the following line <TextBox Text="{Binding Price}"/> This Price property
please take a look at the following snippet: public IEnumerable<T> Query(Expression<Func<T, bool>> filter) {
please take a quick look at this website I get the following error message
Please take a look at the following code: Sort[{1, y, x}, Greater] Max[{1, x,

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.