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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:37:31+00:00 2026-06-10T02:37:31+00:00

I know of the difference between auto , auto& , const auto and const

  • 0

I know of the difference between auto, auto&, const auto and const auto& (for example in a “for each” loop), but one thing that surprised me is:

std::string bla;
const std::string& cf()
{
    return bla;
}


int main (int argc, char *argv[])
{
    auto s1=cf();
    const std::string& s2=cf();
    s1+="XXX"; // not an error
    s2+="YYY"; //error as expected
}

So can somebody tell me when the type of x in the expression auto x = fun(); won’t be the same type as the type of the return value of the fun()?

  • 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-10T02:37:32+00:00Added an answer on June 10, 2026 at 2:37 am

    The rules for auto are the same as for template type deduction:

    template <typename T>
    void f(T t); // same as auto
    template <typename T>
    void g(T& t); // same as auto&
    template <typename T>
    void h(T&& t); // same as auto&&
    
    std::string sv;
    std::string& sl = sv;
    std::string const& scl = sv;
    
    f(sv); // deduces T=std::string
    f(sl); // deduces T=std::string
    f(scl); // deduces T=std::string
    f(std::string()); // deduces T=std::string
    f(std::move(sv)); // deduces T=std::string
    
    g(sv); // deduces T=std::string, T& becomes std::string&
    g(sl); // deduces T=std::string, T& becomes std::string&
    g(scl); // deduces T=std::string const, T& becomes std::string const&
    g(std::string()); // does not compile
    g(std::move(sv)); // does not compile
    
    h(sv); // deduces std::string&, T&& becomes std::string&
    h(sl); // deduces std::string&, T&& becomes std::string&
    h(scl); // deduces std::string const&, T&& becomes std::string const&
    h(std::string()); // deduces std::string, T&& becomes std::string&&
    h(std::move(sv)); // deduces std::string, T&& becomes std::string&&
    

    In general, if you want a copy, you use auto, and if you want a reference you use auto&&. auto&& preserves the constness of the referene and can also bind to temporaries (extending their lifetime).

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

Sidebar

Related Questions

Simple question, but one that I've been curious about...is there a functional difference between
I know one difference between ogg video and mp4 video is that ogg video
I know the difference between the points-to (->) and dot (.) operator but I
I am eager to know the difference between a const variable and a static
I know the difference between synchronized method and synchronized block but I am not
I know the difference in memory usage between byte, unsigned short, and integer, but
I know the difference between long and int But What is the difference between
I know the difference between class methods and object methods but I am not
I know the difference between the two on a technical level. But in real
I know the difference between ASP.NET webforms and ASP.NET MVC and I've seen quite

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.