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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:18:07+00:00 2026-05-23T06:18:07+00:00

Considering the example at the end of the question, is the map object going

  • 0

Considering the example at the end of the question, is the map object going to be created every time the function GetName() is called?
Or is the creation going to be optimized away and created as some lookup table?

#include <iostream>
#include <sstream>
#include <map>
#include <string>
#include <boost/assign/list_of.hpp>

enum abc
{
    A = 1,
    B,
    C
};

std::string GetName( const abc v )
{
    const std::map< abc, std::string > values =
        boost::assign::map_list_of( A, "A" )( B, "B" )( C, "C" );
    std::map< abc, std::string >::const_iterator it = values.find( v );
    if ( values.end() == it )
    {
        std::stringstream ss;
        ss << "invalid value (" << static_cast< int >( v ) << ")";
        return ss.str();
    }
    return it->second;
}

int main()
{
    const abc a = A;
    const abc b = B;
    const abc c = C;
    const abc d = static_cast< abc >( 123 );

    std::cout<<"a="<<GetName(a)<<std::endl;
    std::cout<<"b="<<GetName(b)<<std::endl;
    std::cout<<"c="<<GetName(c)<<std::endl;
    std::cout<<"d="<<GetName(d)<<std::endl;
}
  • 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-23T06:18:08+00:00Added an answer on May 23, 2026 at 6:18 am

    Semantically and conceptually and with respect to The Holy Standard, it will be created every time.

    The rest is up to your compiler and how you support her:

    Possibly the compiler can inline the call and then move deduced invariants outside
    to a single point of initialization.

    Possibly the compiler dislikes that your
    function has external linkage and so does not inline it, then having a hard time
    seeing that invariant from other functions.

    Possibly the compiler will always check a variables constness and use one-time-initialization
    when it can look inside and verify that boost::assign::map_list_of( A, "A" )( B, "B" )( C, "C" )
    does not mutate global state.

    Many factors, and the only way to be sure is to look at generated code.


    In response to a request for quotation:

    3.7.2.3 [basic.std.auto]:

    If a named automatic object has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its copy may be eliminated as specified in”

    This basically means that either it has side effects, in which case it won’t be eliminated, or it hasn’t, in which case it is hardly observable within C++; this means effectively:

    The observed behaviour is always as if it is called every time.

    In other words: There is no way to guarantee that initialization only happens once with automatic storage, so never assume the opposite.

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

Sidebar

Related Questions

considering this example: public static void main(final String[] args) { final List<String> myList =
Im considering use CSLA.NET 3.8 for example for Security and Identity Management on a
Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and a
Considering the set up: Ms Access Application split into Front End and Back End
This is a design question. The design is pseudo-code and represents a small example
I've got a question considering Zend_Controller_Router. I'm using a a modular-structure in my application.
Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
Considering private is the default access modifier for class Members, why is the keyword
Considering this code, can I be absolutely sure that the finally block always executes,

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.