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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:21:37+00:00 2026-05-11T21:21:37+00:00

I’m reviewing a collegue’s code, and I see he has several constants defined in

  • 0

I’m reviewing a collegue’s code, and I see he has several constants defined in the global scope as:

const string& SomeConstant = "This is some constant text";

Personally, this smells bad to me because the reference is referring to what I’m assuming is an “anonymous” object constructed from the given char array.

Syntactically, it’s legal (at least in VC++ 7), and it seems to run, but really I’d rather have him remove the & so there’s no ambiguity as to what it’s doing.

So, is this TRULY safe and legal and I’m obsessing? Does the temp object being constructed have a guaranteed lifetime? I had always assumed anonymous objects used in this manner were destructed after use…


So my question could also be generalized to anonymous object lifetime. Does the standard dictate the lifetime of an anonymous object? Would it have the same lifetime as any other object in that same scope? Or is it only given the lifetime of the expression?


Also, when doing it as a local, it’s obviously scoped differently:

class A
{
    string _str;

public:
    A(const string& str) :
        _str(str)
    {
        cout << "Constructing A(" << _str << ")" << endl;
    }

    ~A()
    {
        cout << "Destructing A(" << _str << ")" << endl;
    }
};

void TestFun()
{
    A("Outer");
    cout << "Hi" << endl;
}

Shows:

Constructing A(Outer);
Destructing A(Outer);
Hi

  • 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-11T21:21:37+00:00Added an answer on May 11, 2026 at 9:21 pm

    It’s completely legal. It will not be destructed until the program ends.

    EDIT: Yes, it’s guaranteed:

    “All objects which do not have dynamic
    storage duration, do not have thread
    storage duration, and are not local
    have static storage duration. The
    storage for these objects shall last
    for the duration of the program
    (3.6.2, 3.6.3).”

    — 2008 Working Draft, Standard for Programming Language C++, § 3.7.1 p. 63

    As Martin noted, this is not the whole answer. The standard draft further notes (§ 12.2, p. 250-1):

    “Temporaries of class type are created
    in various contexts: binding an rvalue
    to a reference (8.5.3) […] Even when
    the creation of the temporary object
    is avoided (12.8), all the semantic
    restrictions shall be respected as if
    the temporary object had been created.
    […] Temporary objects are destroyed
    as the last step in evaluating the
    full-expression (1.9) that (lexically)
    contains the point where they were
    created. […] There are two contexts
    in which temporaries are destroyed at
    a different point than the end of the
    full-expression. […] 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 as specified
    below.”

    I tested in g++ if that makes you feel any better. 😉

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

Sidebar

Ask A Question

Stats

  • Questions 258k
  • Answers 258k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Get the date/time of the next appointment in the database,… May 13, 2026 at 10:56 am
  • Editorial Team
    Editorial Team added an answer The problem is the working directory. When you specify a… May 13, 2026 at 10:56 am
  • Editorial Team
    Editorial Team added an answer don't change the src, just load the images and hide/show/move… May 13, 2026 at 10:56 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.