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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:26:13+00:00 2026-05-26T16:26:13+00:00

Perhaps something similar has already been asked, and sure, it’s a nitpick… I have

  • 0

Perhaps something similar has already been asked, and sure, it’s a nitpick…

I have a bunch of constant std::maps to switch between enum (class) values and their std::string representations (both ways). Someone here pointed out to me that these maps will be initialized at runtime, when other initialization code is run, before my program executes all the good stuff. This would mean constant expressions would impact runtime performance, as the maps are built up from their enum-string pairs.

As an illustrative example, here is an example of one of these maps:

enum class os
{
    Windows,
    Linux,
    MacOSX
};
const map<string, os> os_map =
     { {"windows", os::Windows},
       {"linux",   os::Linux},
       {"mac",     os::MacOSX} };
const map<os, string> os_map_inverse =
     { {os::Windows, "windows"},
       {os::Linux,   "linux"},
       {os::MacOSX,  "mac"} };

Would the C++11 constexpr have any influence on performance, or is my assumption of a runtime initialization penalty false? I would think a compiler can embed a constant STL container as pure data in the executable, but apparently that may not be as easy as I make it sound?

  • 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-26T16:26:14+00:00Added an answer on May 26, 2026 at 4:26 pm

    It’s not so much the performance of initialization that is a problem, but the order of initialization. If someone uses your map before main has started (for example at initialization for a namespace scope variable), then you are SOL, because you are not guaranteed that your map has been initialized before the user’s initialization uses it.

    However you can do this thing at compile time. String literals are constant expressions, as are enumerators. A simple linear-time complexity structure

    struct entry {
      char const *name;
      os value;
    };
    
    constexpr entry map[] = {
      { "windows", os::Windows },
      { "linux", os::Linux },
      { "mac", os::Mac }
    };
    
    constexpr bool same(char const *x, char const *y) {
      return !*x && !*y ? true : (*x == *y && same(x+1, y+1));
    }
    
    constexpr os value(char const *name, entry const *entries) {
      return same(entries->name, name) ? entries->value : value(name, entries+1);
    }
    

    If you use value(a, b) in a constant expression context, and the name you specify doesn’t exist, you will get a compile time error because the function call would become non-constant.

    To use value(a, b) in a non-constant expression context, you better add safety features, like adding an end marker to your array and throwing an exception in value if you hit the end marker (the function call will still be a constant expression as long as you never hit the end marker).

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

Sidebar

Related Questions

Perhaps this has something to do with it being the mainForm, but I'll ask
Something else perhaps? I am already using nHibernate, but I get occasional issues where
Here's something I haven't been able to fix, and I've looked everywhere . Perhaps
I'm looking for a mapping API something which has similar functionality to Google-maps but
I am guessing this question has been asked many times, but i could not
I'm new to visual studio, so perhaps this is something easy... but I've had
Perhaps I'm missing something with the concept of Extension Methods, but I cannot gain
Perhaps I am missing something, but I am just learning javascript. My understanding of
Perhaps I'm missing something simple, as I believe that this is a common scenario...
Perhaps I'm asking for something impossible, but here we go: I'm trying to find

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.