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

  • Home
  • SEARCH
  • 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 3303732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:59:16+00:00 2026-05-17T20:59:16+00:00

A while ago I asked about std::string constants correct idiom for std::string constants? .

  • 0

A while ago I asked about std::string constants correct idiom for std::string constants?.

What I took away from that was not to use std::string constants but to use char string constants. So what the best idiom for that

#define FOO "foo"

const char * const FOO = "foo";

const char FOO[] = "foo";

Desirable features

  • get length at compile time. 1 & 3 but
    not 2 (sizeof doesnt work on 2)
  • can be included in .h without linker
    complaining. all (I think)
  • no multiple copies in .o, in linked
    output. depends on compiler
    (probably)

So it seems like #3 is best but scott meyers says to use #2 (effective c++ item #1)

summary of answers

  1. use jolly complicated template code
  2. use #3

The template code feels like overkill. So for now I go with #3;

But I will ruminate on the template code, the macroized version makes it look OKish; but I dont like the fact that its not portable (who knows, maybe gcc will decide that its wrong too)

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

    Your desired features are contradictory.

    1. Length at compile time
    2. Defined in header file
    3. Single copy across compilation units

    To get (1) and (2), you need to declare the variable as static or put it in an unnamed namespace. However, this will cause a definition in each compilation unit, which goes against (3).

    To get (2) and (3), you need to declare the variable as extern, but then you won’t get (1).

    Now, if your linker is smart, it might optimize away the multiple copies, but I’m not sure if the standard allows it…

    I recommend the const char FOO[] = "foo"; syntax declared in an unnamed namespace or as static if it need to be found in a specific namespace. If the string is very large, then I go for an extern.

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

Sidebar

Related Questions

I was advised a while ago that is was common place to use std::vector
A while ago, I asked a question about $ , and got useful answers
I asked a question while ago about accessing the underlying container of STL adapters
I asked a question a while ago about which local DB was right for
I asked here a while ago about matching text inside of two wrapped <code>..</code>
I asked a little while ago a question about rollback, I have a new
A while ago I asked a question about hierarchy/version number sorting in SQL Server.
I asked this question a while ago and got an answer that I thought
A while ago, I came across some code that marked a data member of
A while ago I had a query that I ran quite a lot for

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.