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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:21:39+00:00 2026-05-29T09:21:39+00:00

In template <typename T> T const & foo(T const & dflt) { return /*

  • 0

In

template <typename T>
T const & foo(T const & dflt) 
  { return /* ... */ ? /* ... */ : dflt; }

int x = foo(5);

Does the reference to temporary “survive” until it’s assigned to x?

(I know it survives the duration of the foo() call, but the assignment makes me hesitate)

The code in question has some legitimate uses for relying on const references here, i.e. I would need two separate functions

T const & foo_cr(T  const & dflt);
T const & foo_v(T dflt);

which I want to avoid (just relying on different signatures wiht the same name seems a bit risky to me.)

  • 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-29T09:21:39+00:00Added an answer on May 29, 2026 at 9:21 am

    Does the reference to temporary “survive” until it’s assigned to x?

    Generally, yes. If foo returns dflt, that reference (to a temporary 5) is valid until the end of the full-expression it was created in, that means, until after the initialization.

    C++03, 12.2/3:

    When an implementation introduces a temporary object of a class that
    has a non-trivial constructor (12.1), it shall ensure that a
    constructor is called for the temporary object. Similarly, the
    destructor shall be called for a temporary with a non-trivial
    destructor (12.4). Temporary objects are destroyed as the last step in
    evaluating the full-expression (1.9) that (lexically) contains the
    point where they were created. This is true even if that evaluation
    ends in throwing an exception.

    More comes in 12.2/4 and 12.2/5, especially the exceptions of this rule.

    I can’t see what happens when it returns the ... you left out, but I guess you weren’t asking about this.

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

Sidebar

Related Questions

I want to do something like template <typename T> void foo(const T& t) {
template<typename T, size_t n> size_t array_size(const T (&)[n]) { return n; } The part
I have a template function: template<typename T> void foo(const T& value) { bar(value); x
Suppose I have a function template: template <typename T> std::string foo(const T& x) {
Say I have this: template<typename T, int X> class foo { public: void set(const
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
The following code: template <typename S, typename T> struct foo { void bar(); };
I have a class: class foo { private: std::string data; public: foo &append(const char*
Consider the following small example: template<typename T> void foo(T a) { ... } struct

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.