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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:24:41+00:00 2026-05-13T13:24:41+00:00

I want a header file with a non-integral constant in it, e.g. a class.

  • 0

I want a header file with a non-integral constant in it, e.g. a class. Note the constant does not need to be a compile-time constant.

static const std::string Ten = "10";

This compiles but is undesirable as each compilation unit now has its own copy of Ten.

const std::string Ten = "10";

This will compile but will fail with a linker error for multiply defined Ten.

constexpr std::string Ten = "10"s;

This would work but only if the strings constructor was constexpr as well. It will be but I can’t count on every non-integral constant to have a constexpr constructor … or can I?

extern const std::string Ten = "10";

This seems to work but I’m afraid I’ll get a linker error if I breath on it wrong.

inline const std::string Ten( ) { return "10"; }

This has everything I want except a clean syntax. Plus now I have to refer the constant as a function call, Ten().

inline const std::string = "10";

This seems to be the ideal solution. Of course inline variables aren’t allowed by the standard.

  • Is there something in the c++ standard that says the extern version should work or am I just lucky it works with GCC?
  • Is there a compelling reason not to allow inline variables?
  • Is there a better way with c++03 or will there be a better way in c++0x?
  • 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-13T13:24:41+00:00Added an answer on May 13, 2026 at 1:24 pm

    You seem to have them mixed up.

    You are right about

    static const std::string Ten = "10"; 
    

    version. It will “work”, but it will create a separate object in each translation unit.

    The version without static will have the same effect. It won’t produce linker errors, but will define a separate object in each translation unit. In C++ language const objects have internal linkage by default, meaning that

    const std::string Ten = "10"; // `static` is optional
    

    is exactly equivalent to the previous version with static.

    The version with extern and initializer

    extern const std::string Ten = "10"; // it's a definition!
    

    will produce a definition of an object with external linkage (it is a definition because of the presence of an initializer). This version will result in linker errors, since you’ll end up with multiple definitions of an object with external linkage – a violation of ODR.

    Here’s how you can do it:

    In order to achieve what you are trying to achieve, you have to declare your constant in the header file

    extern const std::string Ten; // non-defining declaration
    

    and then define it (with initializer) in one and only one of the implementation files

    extern const std::string Ten = "10"; // definition, `extern` optional
    

    (If the constant is pre-declared as extern, then extern in the definition is optional. Even without an explicit extern it will define a const object with external linkage.)

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

Sidebar

Related Questions

I have a c++ header file containing a class. I want to use this
I need to concatenate 3 files using C#. A header file, content, and a
I love to organize my code, so ideally I want one class per file
I want to add a row of headers to an existing CSV file, editing
I want to capture the HTTP request header fields, primarily the Referer and User-Agent,
I want to create a simple box with a header bar containing a title
Want to know what the stackoverflow community feels about the various free and non-free
I want to loop over the contents of a text file and do a
I'm writing a C++ class to read input from a file into preallocated buffers
I'm binding a query to a WinForms DataGridView . I want the column headers

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.