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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:48:14+00:00 2026-05-13T20:48:14+00:00

I have a map that represents a DB object. I want to get ‘well

  • 0

I have a map that represents a DB object. I want to get ‘well known’ values from it

 std::map<std::string, std::string> dbo;
 ...
 std::string val = map["foo"];

all fine but it strikes me that “foo” is being converted to a temporary string on every call. Surely it would be better to have a constant std::string (of course its probably a tiny overhead compared to the disk IO that just fetched the object but its still a valid question I think). So what is the correct idiom for std::string constants?

for example – I can have

 const std::string FOO = "foo";

in a hdr, but then I get multiple copies

EDIT: No answer yet has said how to declare std::string constants. Ignore the whole map, STL, etc issue. A lot of code is heavily std::string oriented (mine certainly is) and it is natural to want constants for them without paying over and over for the memory allocation

EDIT2: took out secondary question answered by PDF from Manuel, added example of bad idiom

EDIT3: Summary of answers. Note that I have not included those that suggested creating a new string class. I am disappointed becuase I hoped there was a simple thing that would work in header file only (like const char * const ). Anyway

a) from Mark b

 std::map<int, std::string> dict;
 const int FOO_IDX = 1;
 ....
 dict[FOO_IDX] = "foo";
 ....
 std:string &val = dbo[dict[FOO_IDX]];

b) from vlad

 // str.h
 extern const std::string FOO;
 // str.cpp
 const std::string FOO = "foo";

c) from Roger P

 // really you cant do it

(b) seems the closest to what I wanted but has one fatal flaw. I cannot have static module level code that uses these strings since they might not have been constructed yet. I thought about (a) and in fact use a similar trick when serializing the object, send the index rather than the string, but it seemed a lot of plumbing for a general purpose solution. So sadly (c) wins, there is not simple const idiom for std:string

  • 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-13T20:48:14+00:00Added an answer on May 13, 2026 at 8:48 pm

    The copying and lack of “string literal optimization” is just how std::strings work, and you cannot get exactly what you’re asking for. Partially this is because virtual methods and dtor were explicitly avoided. The std::string interface is plenty complicated without those, anyway.

    The standard requires a certain interface for both std::string and std::map, and those interfaces happen to disallow the optimization you’d like (as “unintended consequence” of its other requirements, rather than explicitly). At least, they disallow it if you want to actually follow all the gritty details of the standard. And you really do want that, especially when it is so easy to use a different string class for this specific optimization.

    However, that separate string class can solve these “problems” (as you said, it’s rarely an issue), but unfortunately the world has number_of_programmers + 1 of those already. Even considering that wheel reinvention, I have found it useful to have a StaticString class, which has a subset of std::string’s interface: using begin/end, substr, find, etc. It also disallows modification (and fits in with string literals that way), storing only a char pointer and a size. You have to be slightly careful that it’s only initialized with string literals or other “static” data, but that is somewhat mitigated by the construction interface:

    struct StaticString {
      template<int N>
      explicit StaticString(char (&data)[N]); // reference to char array
      StaticString(StaticString const&); // copy ctor (which is very cheap)
    
      static StaticString from_c_str(char const* c_str); // static factory function
      // this only requires that c_str not change and outlive any uses of the
      // resulting object(s), and since it must also be called explicitly, those 
      // requirements aren't hard to enforce; this is provided because it's explicit
      // that strlen is used, and it is not embedded-'\0'-safe as the
      // StaticString(char (&data)[N]) ctor is
    
      operator char const*() const; // implicit conversion "operator"
      // here the conversion is appropriate, even though I normally dislike these
    
    private:
      StaticString(); // not defined
    };
    

    Use:

    StaticString s ("abc");
    assert(s != "123"); // overload operators for char*
    some_func(s); // implicit conversion
    some_func(StaticString("abc")); // temporary object initialized from literal
    

    Note the primary advantage of this class is explicitly to avoid copying string data, so the string literal storage can be reused. There’s a special place in the executable for this data, and it is generally well optimized as it dates back from the earliest days of C and beyond. In fact, I feel this class is close to what string literals should’ve been in C++, if it weren’t for the C compatibility requirement.

    By extension, you could also write your own map class if this is a really common scenario for you, and that could be easier than changing string types.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found out that this is usually done with the… May 13, 2026 at 11:12 pm
  • Editorial Team
    Editorial Team added an answer You can use a constructor: v_obj myType := new myType(); May 13, 2026 at 11:12 pm
  • Editorial Team
    Editorial Team added an answer To answer the question (Why does Enter or Tab key… May 13, 2026 at 11:12 pm

Related Questions

I have a set of N^2 numbers and N bins. Each bin is supposed
I'm looking for some suggestions as to how I could implement a World Map
I have a number of tracks recorded by a GPS, which more formally can
I'm displaying a small Google map on a web page using the Google Maps
I have a program which gets commands as a string. Each character in the

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.