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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:07:45+00:00 2026-06-11T01:07:45+00:00

I am developing a library that will consist of header files only. So far,

  • 0

I am developing a library that will consist of header files only. So far, it contains only classes, which has been fine. However, I have come to a point where I need to have some library-wide accessible unchanging data in the library (that is, not class instance data) for the implementation of some functions. You obviously can’t just put global data in header files, or else every compilation unit that #includes the header will have a definition for the symbol and you’ll get multiple definition errors at link-time.

I seem to have found a workaround that lets me have static data in a class without having to add a compilation unit to the library by just making the data a static variable in a function and returning a pointer to that data:

class StaticData {
public:
    void doSomething() { /* this uses getData */ }
    void doSomethingElse() { /* this does too */ }

private:
    static int* getData() {
        static int array[] { 1, 2, 3, 4 };

        return array;
    }
};

This appears to be working fine, but I must admit that I don’t know what happens to function-static data in inline functions in header files. I am wondering if this “hack” has any unintended repercussions, such as every compilation unit that #includes this header getting its own version of array. How and where does the compiler decide to put it?

Also it should be noted that I am not using this to implement the singleton antipattern or anything. I am just using it to store data that multiple functions will need to use (which is why it can’t be static in just a function that uses it, but even if it did, that would prompt the same question).

  • 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-11T01:07:47+00:00Added an answer on June 11, 2026 at 1:07 am

    That’s fine. It’s guaranteed that there will only be one copy of array, as long as the function has external linkage, which this does. The C++ standard says:

    7.1.2/4 A static local variable in an extern inline function always refers to the same object.

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

Sidebar

Related Questions

I'm developing a library project that will be integrated into some popular android applications
I am developing a class library (C#) that i will use it for my
I'm currently in the process of developing a fairly large static library which will
I am developing a library that consists of several individual classes that all work
I'm developing a static library that will be distributed to other developers, who may
I am developing a class library that will be used in several projects. In
I'm developing a class library in .NET that other developers will consume eventually. This
I'm about to set out on developing a .NET class library that will be
I have been developing a library that is getting pretty large, and now I
I'm developing a class library that will be used in a number of different

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.