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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:39:54+00:00 2026-06-17T17:39:54+00:00

local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string&

  • 0

local lvalue references-to-const and rvalue references can extend the lifetime of temporaries:

const std::string& a = std::string("hello");
std::string&& b = std::string("world");

Does that also work when the initializer is not a simple expression, but uses the conditional operator?

std::string&& c = condition ? std::string("hello") : std::string("world");

What if one of the results is a temporary object, but the other one isn’t?

std::string d = "hello";
const std::string& e = condition ? d : std::string("world");

Does C++ mandate the lifetime of the temporary be extended when the condition is false?

The question came up while answering this question about non-copyable objects.

  • 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-17T17:39:56+00:00Added an answer on June 17, 2026 at 5:39 pm

    Both of those are fine.

    §5.16 says (extraordinarily abridged):

    2 If either the second or the third operand has type void

    Nope.

    3 Otherwise, if the second and third operand have different types

    Nope.

    4 If the second and third operands are glvalues of the same value category

    Nope. (In the first, both are prvalues and in the second one is a glvalue and one is a prvalue.)

    5 Otherwise, the result is a prvalue

    Okay, so both of these result in prvalues. So the binding is fine, but what’s the binding to?

    6 Lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are per- formed on the second and third operands.

    Okay, so both are now rvalues if they weren’t already.

    6 (continued) After those conversions, one of the following shall hold:

    The second and third operands have the same type; the result is of that type. If the operands have class type, the result is a prvalue temporary of the result type, which is copy-initialized from either the second operand or the third operand depending on the value of the first operand.

    Okay, so it’s either std::string(first_operand) or std::string(second_operand).

    Regardless, the result of the conditional expression is a new prvalue temporary, and it’s that value that’s extended by binding to your references.

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

Sidebar

Related Questions

#!/usr/local/bin/perl use warnings; use strict; print Hello, world!\n; This is the file I have
#!/usr/local/bin/perl use warnings; use strict; use CGI qw(:standard); print header; print start_html(-title=>Hello World, -bgcolor=>#cccccc,
A local maximum in a 2D array can be defined as a value such
OK, very simple String class, which holds constant strings (i.e. can't be changed once
local szMsg = string.format( 歡迎來到非常劍俠有獎問答,您隻需交納%s就可以參加本次有獎答題,隻要您能回答正確<color=yellow>%s個<color>關於劍俠世界的小問題,就會獲得%s,如果中途答錯或者自己選擇退出,不僅沒有獎勵,您交納的%s也不會退還。怎麼樣,要參加嗎?, szNeedMoney, self.tbGroup[nGroupId].nQuestionMax, szAwardMsg, szNeedMoney); local tbOpt = I want
Using (local) in the connection string doesn't work on my cluster. I'm assuming it's
local uLong unzlocal_SearchCentralDir OF(( const zlib_filefunc_def* pzlib_filefunc_def, voidpf filestream)); local uLong unzlocal_SearchCentralDir(pzlib_filefunc_def,filestream) const zlib_filefunc_def*
Named local classes are very rarely used, usually local classes are anonymous. Does anybody
local invoiceData = [[I N V O I C E Invoice No. : ABCDEFG125469857
Using local variables seems advisable in a partial that could be used application-wide to

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.