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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:47:33+00:00 2026-06-17T15:47:33+00:00

From everything I am reading and testing, there is no way (without a preprocessor

  • 0

From everything I am reading and testing, there is no way (without a preprocessor macro) to define a constant in a shared header and ensure that each TU is not creating its own storage for that constant.

I can do this:

const int maxtt=888888;

Which is the same exactly as:

static const int maxtt=888888;

And if this header is shared, it will work but each TU gets its own copy of maxtt. I could also do this, to prevent that:

extern const int maxtt;

But then I cannot define maxtt here; that must be done in a CPP to avoid linker error.

Is my understanding correct?

  • 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-06-17T15:47:34+00:00Added an answer on June 17, 2026 at 3:47 pm

    Since the variable is constant, the fact that each TU gets its own copy is usually irrelevant.

    In C++, constants at namespace scope are implicitly static for this reason. Often this allows for better code than if you only had a single instance with external linkage, since (if the variable is actually a constant expression) the constant can often be folded right into the usage site and doesn’t need to be stored at all.

    So unless you really need to take the address of the constant, or something like that, you should stick with the static version. (And as you already observed, you can force external linkage by adding extern.) Another reason may be that you’re initializing dynamically and only want one call to the initializer:

    // header:
    extern int const n;
    
    // one implementation:
    int const n = init_function_with_side_effects();
    

    The static construction (int const n = init(); in the header) would cause the function to be called once in every TU.

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

Sidebar

Related Questions

Based on everything I've been reading, there is no way to get the current
Well is there? From everything I've read, it seems like the answer is no,but
I'm developing a touchscreen application that, aside from everything else, records the amount of
I am creating a php backup script that will dump everything from a database
Is there a one-line easy linq expression to just get everything from a simple
I`m working on an app that does reading and handling specific URIs from NFC
From what I can tell from reading everything I can find here, this doesn't
From reading a few questions and answers here, it seems that a telnet stream
From what I understood, by reading the solrcloud examples is that you can declare
I want to read everything from a textfile and echo it. But there might

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.