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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:48:57+00:00 2026-05-27T13:48:57+00:00

When I use C++11 auto , what are the rules of type deduction with

  • 0

When I use C++11 auto, what are the rules of type deduction with regards to whether it will resolve to a value or a reference?

E.g, sometimes it is clear:

auto i = v.begin(); // Copy, begin() returns an iterator by value

These are less clear:

const std::shared_ptr<Foo>& get_foo();
auto p = get_foo(); // Copy or reference?

static std::shared_ptr<Foo> s_foo;
auto sp = s_foo; // Copy or reference?

std::vector<std::shared_ptr<Foo>> c;
for (auto foo: c) { // Copy for every loop iteration?
  • 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-27T13:48:57+00:00Added an answer on May 27, 2026 at 1:48 pm

    The rule is simple : it is how you declare it.

    int i = 5;
    auto a1 = i;    // value
    auto & a2 = i;  // reference
    

    Next example proves it :

    #include <typeinfo>
    #include <iostream>    
    
    template< typename T >
    struct A
    {
        static void foo(){ std::cout<< "value" << std::endl; }
    };
    template< typename T >
    struct A< T&>
    {
        static void foo(){ std::cout<< "reference" << std::endl; }
    };
    
    float& bar()
    {
        static float t=5.5;
        return t;
    }
    
    int main()
    {
        int i = 5;
        int &r = i;
    
        auto a1 = i;
        auto a2 = r;
        auto a3 = bar();
    
        A<decltype(i)>::foo();       // value
        A<decltype(r)>::foo();       // reference
        A<decltype(a1)>::foo();      // value
        A<decltype(a2)>::foo();      // value
        A<decltype(bar())>::foo();   // reference
        A<decltype(a3)>::foo();      // value
    }
    

    The output:

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

Sidebar

Related Questions

I normally use CSS rules for margin:0 auto along with a 960 container for
I've been trying to use auto return type templates and am having trouble. I
I am planning to use Auto reset Event Handle for Inter Thread communication. EventWaitHandle
In a non-sharded DB, I could just use auto-increment to generate a unique ID
In my Asp.Net project I wanna use Property Auto-wiring, e.g. for my ILogger. Basically
Hey, I'm just wondering what code I could use to auto change the size
For technical reasons, I can't use ClickOnce to auto-update my .NET application and its
We use the following function to auto detect if we are on a machine
I am trying to use Auto-implemented properties in VS2005. I have .NET 3.0 framework
We use Auto Scaling and it works pretty good for us, but this morning

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.