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

  • Home
  • SEARCH
  • 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 9211235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:14:58+00:00 2026-06-18T01:14:58+00:00

Possible Duplicate: how to initialize function arguments that are classes with default value #include

  • 0

Possible Duplicate:
how to initialize function arguments that are classes with default value

#include <string>

void foo1(const std::string& s = std::string());

void foo2(std::string& s = std::string());

void foo3(const std::string s = std::string());

void foo4(std::string s = std::string());

error at foo2(): default argument for ‘std::string& s’ has type ‘std::string {aka std::basic_string<char>}’

I understand the compiler’s point, but I don’t get how this does not apply to foo1() as well.

  • 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-18T01:14:59+00:00Added an answer on June 18, 2026 at 1:14 am

    You can’t take a non-const reference to a temporary like foo2 does.

    Notice that this isn’t specifically default parameters. You get the same error for function variables: http://ideone.com/g7Tf7L

    #include <string>
    using std::string;
    
    #include <iostream>
    using std::cout; using std::endl;
    
    int main()
    {
        string s1        = string("s1"); // OK, copy it
        const string& s2 = string("s2"); // OK, const reference to it
        string& s3       = string("s3"); // ERROR! non-const reference not allowed!
    
        cout
                << s1 << ", "
                << s2 << ", "
                << s3 << endl;
        return 0;
    }
    

    When you take a const reference to a temporary, the lifetime of the temporary is extended to the lifetime of the reference (§12.2, quoted from my copy of C++11 draft n3337):

    There are two contexts in which temporaries are destroyed at a different point than the end of the fullexpression.

    …

    The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:

    • A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits.
    • A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.
    • The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.
    • A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the full-expression containing the new-initializer.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
Possible Duplicate: Initialize list with same bool value I'm attempting to make a prime
Possible Duplicate: Casting a function pointer to another type Assume i initialize a function
Possible Duplicate: How to initialize a list to a certain value in Python In
Possible Duplicate: Passing multidimensional arrays as function arguments in C I'm trying to use
Possible Duplicate: Is it a bug that Microsoft VS C++ compiler can Initialize a
Possible Duplicate: How to initialize a const field in constructor? I have this class:
Possible Duplicate: initialize a const array in a class initializer in C++ If I
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Recommended way to initialize srand? I am extracting frames from AVI. I

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.