How to have static data members in a header-only library?










41















What is the best way to have a static member in a non-templated library class,
without placing the burden of defining the member on the class user?



Say I want to provide this class:



class i_want_a_static_member

static expensive_resource static_resource_;

public:
void foo()

static_resource_.bar();

;


Then the user of the class must not forget to define the static member somewhere
(as already answered many times):



// this must be done somewhere in a translation unit
expensive_resource i_want_a_static_member::static_resource_;


I do have an answer below, but it has some disadvantages. Are there better and/or more elegant solutions?










share|improve this question
























  • When you say "non-templated", do you mean you are forced not to use any templates, or just that the main classes don't happen to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:12











  • @VaughnCato I just don't want the class user have to deal with a templated class. Maybe it just makes no sense to introduce a template parameter for the class i_want_a_static_member.

    – pesche
    Jul 29 '12 at 14:15











  • Ok, but if it is a helper class that the user doesn't have to deal with, then it is ok for it to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:16











  • @VaughnCato Yes, that's okay. You can see in my own answer that I'm using a templated helper class, too. But I want to provide a class that the user can deal with (that's the reason for providing it).

    – pesche
    Jul 29 '12 at 14:20











  • Yes, I see. Another thing you can do is use an inline member function with a static local variable and the member function just returns a reference to it.

    – Vaughn Cato
    Jul 29 '12 at 14:49















41















What is the best way to have a static member in a non-templated library class,
without placing the burden of defining the member on the class user?



Say I want to provide this class:



class i_want_a_static_member

static expensive_resource static_resource_;

public:
void foo()

static_resource_.bar();

;


Then the user of the class must not forget to define the static member somewhere
(as already answered many times):



// this must be done somewhere in a translation unit
expensive_resource i_want_a_static_member::static_resource_;


I do have an answer below, but it has some disadvantages. Are there better and/or more elegant solutions?










share|improve this question
























  • When you say "non-templated", do you mean you are forced not to use any templates, or just that the main classes don't happen to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:12











  • @VaughnCato I just don't want the class user have to deal with a templated class. Maybe it just makes no sense to introduce a template parameter for the class i_want_a_static_member.

    – pesche
    Jul 29 '12 at 14:15











  • Ok, but if it is a helper class that the user doesn't have to deal with, then it is ok for it to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:16











  • @VaughnCato Yes, that's okay. You can see in my own answer that I'm using a templated helper class, too. But I want to provide a class that the user can deal with (that's the reason for providing it).

    – pesche
    Jul 29 '12 at 14:20











  • Yes, I see. Another thing you can do is use an inline member function with a static local variable and the member function just returns a reference to it.

    – Vaughn Cato
    Jul 29 '12 at 14:49













41












41








41


4






What is the best way to have a static member in a non-templated library class,
without placing the burden of defining the member on the class user?



Say I want to provide this class:



class i_want_a_static_member

static expensive_resource static_resource_;

public:
void foo()

static_resource_.bar();

;


Then the user of the class must not forget to define the static member somewhere
(as already answered many times):



// this must be done somewhere in a translation unit
expensive_resource i_want_a_static_member::static_resource_;


I do have an answer below, but it has some disadvantages. Are there better and/or more elegant solutions?










share|improve this question
















What is the best way to have a static member in a non-templated library class,
without placing the burden of defining the member on the class user?



Say I want to provide this class:



class i_want_a_static_member

static expensive_resource static_resource_;

public:
void foo()

static_resource_.bar();

;


Then the user of the class must not forget to define the static member somewhere
(as already answered many times):



// this must be done somewhere in a translation unit
expensive_resource i_want_a_static_member::static_resource_;


I do have an answer below, but it has some disadvantages. Are there better and/or more elegant solutions?







c++ static-members header-only






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:02









Community

11




11










asked Jul 29 '12 at 14:06









peschepesche

1,62412434




1,62412434












  • When you say "non-templated", do you mean you are forced not to use any templates, or just that the main classes don't happen to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:12











  • @VaughnCato I just don't want the class user have to deal with a templated class. Maybe it just makes no sense to introduce a template parameter for the class i_want_a_static_member.

    – pesche
    Jul 29 '12 at 14:15











  • Ok, but if it is a helper class that the user doesn't have to deal with, then it is ok for it to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:16











  • @VaughnCato Yes, that's okay. You can see in my own answer that I'm using a templated helper class, too. But I want to provide a class that the user can deal with (that's the reason for providing it).

    – pesche
    Jul 29 '12 at 14:20











  • Yes, I see. Another thing you can do is use an inline member function with a static local variable and the member function just returns a reference to it.

    – Vaughn Cato
    Jul 29 '12 at 14:49

















  • When you say "non-templated", do you mean you are forced not to use any templates, or just that the main classes don't happen to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:12











  • @VaughnCato I just don't want the class user have to deal with a templated class. Maybe it just makes no sense to introduce a template parameter for the class i_want_a_static_member.

    – pesche
    Jul 29 '12 at 14:15











  • Ok, but if it is a helper class that the user doesn't have to deal with, then it is ok for it to be templated?

    – Vaughn Cato
    Jul 29 '12 at 14:16











  • @VaughnCato Yes, that's okay. You can see in my own answer that I'm using a templated helper class, too. But I want to provide a class that the user can deal with (that's the reason for providing it).

    – pesche
    Jul 29 '12 at 14:20











  • Yes, I see. Another thing you can do is use an inline member function with a static local variable and the member function just returns a reference to it.

    – Vaughn Cato
    Jul 29 '12 at 14:49
















When you say "non-templated", do you mean you are forced not to use any templates, or just that the main classes don't happen to be templated?

– Vaughn Cato
Jul 29 '12 at 14:12





When you say "non-templated", do you mean you are forced not to use any templates, or just that the main classes don't happen to be templated?

– Vaughn Cato
Jul 29 '12 at 14:12













@VaughnCato I just don't want the class user have to deal with a templated class. Maybe it just makes no sense to introduce a template parameter for the class i_want_a_static_member.

– pesche
Jul 29 '12 at 14:15





@VaughnCato I just don't want the class user have to deal with a templated class. Maybe it just makes no sense to introduce a template parameter for the class i_want_a_static_member.

– pesche
Jul 29 '12 at 14:15













Ok, but if it is a helper class that the user doesn't have to deal with, then it is ok for it to be templated?

– Vaughn Cato
Jul 29 '12 at 14:16





Ok, but if it is a helper class that the user doesn't have to deal with, then it is ok for it to be templated?

– Vaughn Cato
Jul 29 '12 at 14:16













@VaughnCato Yes, that's okay. You can see in my own answer that I'm using a templated helper class, too. But I want to provide a class that the user can deal with (that's the reason for providing it).

– pesche
Jul 29 '12 at 14:20





@VaughnCato Yes, that's okay. You can see in my own answer that I'm using a templated helper class, too. But I want to provide a class that the user can deal with (that's the reason for providing it).

– pesche
Jul 29 '12 at 14:20













Yes, I see. Another thing you can do is use an inline member function with a static local variable and the member function just returns a reference to it.

– Vaughn Cato
Jul 29 '12 at 14:49





Yes, I see. Another thing you can do is use an inline member function with a static local variable and the member function just returns a reference to it.

– Vaughn Cato
Jul 29 '12 at 14:49












3 Answers
3






active

oldest

votes


















43














You can use function local static variables.



struct Foo 
static int& Bar() static int I; return I;
; // ^~~~~~~~~~~~





share|improve this answer


















  • 2





    Could someone explain why this works?

    – Srinath Sridhar
    Aug 12 '13 at 18:33






  • 5





    @SrinathSridhar: Not much to explain, this is just a feature of C++. When a variable at function scope is declared with the static storage qualifier then the language that one and only one instance is created. This instance is lazy-initialized the first time that flow-control pass through its declaration, deterministically.

    – Matthieu M.
    Aug 12 '13 at 18:38






  • 3





    This is not thread safe until C++11 spec. Even now that the spec is out, not all compilers support thread safe static initialization yet. For example, neither MS Visual Studio 2012 or 2013 support what they call "magic statics".

    – Micah Zoltu
    Aug 19 '13 at 0:21






  • 4





    @MicahCaldwell: Thanks for the remark, I have not been using Visual Studio for ages. It's quite unfortunate since it's been thread-safe in gcc for a long time (even before C++11).

    – Matthieu M.
    Aug 19 '13 at 6:28


















16














My own solution is to use a templated holder class, as static members work fine in templates, and use this holder as a base class.



template <typename T>
struct static_holder

static T static_resource_;
;

template <typename T>
T static_holder<T>::static_resource_;


Now use the holder class:



class expensive_resource /*...*/ ;

class i_want_a_static_member : private static_holder<expensive_resource>

public:
void foo()

static_resource_.bar();

;


But as the name of the member is specified in the holder class, you can't use the same holder for more than one static member.






share|improve this answer


















  • 6





    Note: actually, you could use composition instead of inheritance. struct A static_holder<B> x; static_holder<B> y; ; would work, although it would not take advantage of Empty Base Optimization.

    – Matthieu M.
    Aug 13 '13 at 6:12


















3














As of C++ 17. You can now use inline variables to do this:



static const inline float foo = 1.25f;





share|improve this answer























  • On behalf of header purists everywhere, THANK YOU!

    – Syndog
    Dec 21 '18 at 19:23











  • Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable. See en.cppreference.com/w/cpp/language/inline. Also I would drop "const" from this answer as it's a special case.

    – ARA1307
    Dec 25 '18 at 2:58











  • Can you elaborate on where you get "Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable." from your cppreference link? I skimmed over it and didn't see where that came from.

    – redfeatherplusplus
    Jan 4 at 17:30












  • sorry, but I think that "upgrade to C++17" is not a viable way 99% of the time. Good point, though.

    – fiorentinoing
    Feb 8 at 12:55











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11709859%2fhow-to-have-static-data-members-in-a-header-only-library%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









43














You can use function local static variables.



struct Foo 
static int& Bar() static int I; return I;
; // ^~~~~~~~~~~~





share|improve this answer


















  • 2





    Could someone explain why this works?

    – Srinath Sridhar
    Aug 12 '13 at 18:33






  • 5





    @SrinathSridhar: Not much to explain, this is just a feature of C++. When a variable at function scope is declared with the static storage qualifier then the language that one and only one instance is created. This instance is lazy-initialized the first time that flow-control pass through its declaration, deterministically.

    – Matthieu M.
    Aug 12 '13 at 18:38






  • 3





    This is not thread safe until C++11 spec. Even now that the spec is out, not all compilers support thread safe static initialization yet. For example, neither MS Visual Studio 2012 or 2013 support what they call "magic statics".

    – Micah Zoltu
    Aug 19 '13 at 0:21






  • 4





    @MicahCaldwell: Thanks for the remark, I have not been using Visual Studio for ages. It's quite unfortunate since it's been thread-safe in gcc for a long time (even before C++11).

    – Matthieu M.
    Aug 19 '13 at 6:28















43














You can use function local static variables.



struct Foo 
static int& Bar() static int I; return I;
; // ^~~~~~~~~~~~





share|improve this answer


















  • 2





    Could someone explain why this works?

    – Srinath Sridhar
    Aug 12 '13 at 18:33






  • 5





    @SrinathSridhar: Not much to explain, this is just a feature of C++. When a variable at function scope is declared with the static storage qualifier then the language that one and only one instance is created. This instance is lazy-initialized the first time that flow-control pass through its declaration, deterministically.

    – Matthieu M.
    Aug 12 '13 at 18:38






  • 3





    This is not thread safe until C++11 spec. Even now that the spec is out, not all compilers support thread safe static initialization yet. For example, neither MS Visual Studio 2012 or 2013 support what they call "magic statics".

    – Micah Zoltu
    Aug 19 '13 at 0:21






  • 4





    @MicahCaldwell: Thanks for the remark, I have not been using Visual Studio for ages. It's quite unfortunate since it's been thread-safe in gcc for a long time (even before C++11).

    – Matthieu M.
    Aug 19 '13 at 6:28













43












43








43







You can use function local static variables.



struct Foo 
static int& Bar() static int I; return I;
; // ^~~~~~~~~~~~





share|improve this answer













You can use function local static variables.



struct Foo 
static int& Bar() static int I; return I;
; // ^~~~~~~~~~~~






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 29 '12 at 16:53









Matthieu M.Matthieu M.

204k29278517




204k29278517







  • 2





    Could someone explain why this works?

    – Srinath Sridhar
    Aug 12 '13 at 18:33






  • 5





    @SrinathSridhar: Not much to explain, this is just a feature of C++. When a variable at function scope is declared with the static storage qualifier then the language that one and only one instance is created. This instance is lazy-initialized the first time that flow-control pass through its declaration, deterministically.

    – Matthieu M.
    Aug 12 '13 at 18:38






  • 3





    This is not thread safe until C++11 spec. Even now that the spec is out, not all compilers support thread safe static initialization yet. For example, neither MS Visual Studio 2012 or 2013 support what they call "magic statics".

    – Micah Zoltu
    Aug 19 '13 at 0:21






  • 4





    @MicahCaldwell: Thanks for the remark, I have not been using Visual Studio for ages. It's quite unfortunate since it's been thread-safe in gcc for a long time (even before C++11).

    – Matthieu M.
    Aug 19 '13 at 6:28












  • 2





    Could someone explain why this works?

    – Srinath Sridhar
    Aug 12 '13 at 18:33






  • 5





    @SrinathSridhar: Not much to explain, this is just a feature of C++. When a variable at function scope is declared with the static storage qualifier then the language that one and only one instance is created. This instance is lazy-initialized the first time that flow-control pass through its declaration, deterministically.

    – Matthieu M.
    Aug 12 '13 at 18:38






  • 3





    This is not thread safe until C++11 spec. Even now that the spec is out, not all compilers support thread safe static initialization yet. For example, neither MS Visual Studio 2012 or 2013 support what they call "magic statics".

    – Micah Zoltu
    Aug 19 '13 at 0:21






  • 4





    @MicahCaldwell: Thanks for the remark, I have not been using Visual Studio for ages. It's quite unfortunate since it's been thread-safe in gcc for a long time (even before C++11).

    – Matthieu M.
    Aug 19 '13 at 6:28







2




2





Could someone explain why this works?

– Srinath Sridhar
Aug 12 '13 at 18:33





Could someone explain why this works?

– Srinath Sridhar
Aug 12 '13 at 18:33




5




5





@SrinathSridhar: Not much to explain, this is just a feature of C++. When a variable at function scope is declared with the static storage qualifier then the language that one and only one instance is created. This instance is lazy-initialized the first time that flow-control pass through its declaration, deterministically.

– Matthieu M.
Aug 12 '13 at 18:38





@SrinathSridhar: Not much to explain, this is just a feature of C++. When a variable at function scope is declared with the static storage qualifier then the language that one and only one instance is created. This instance is lazy-initialized the first time that flow-control pass through its declaration, deterministically.

– Matthieu M.
Aug 12 '13 at 18:38




3




3





This is not thread safe until C++11 spec. Even now that the spec is out, not all compilers support thread safe static initialization yet. For example, neither MS Visual Studio 2012 or 2013 support what they call "magic statics".

– Micah Zoltu
Aug 19 '13 at 0:21





This is not thread safe until C++11 spec. Even now that the spec is out, not all compilers support thread safe static initialization yet. For example, neither MS Visual Studio 2012 or 2013 support what they call "magic statics".

– Micah Zoltu
Aug 19 '13 at 0:21




4




4





@MicahCaldwell: Thanks for the remark, I have not been using Visual Studio for ages. It's quite unfortunate since it's been thread-safe in gcc for a long time (even before C++11).

– Matthieu M.
Aug 19 '13 at 6:28





@MicahCaldwell: Thanks for the remark, I have not been using Visual Studio for ages. It's quite unfortunate since it's been thread-safe in gcc for a long time (even before C++11).

– Matthieu M.
Aug 19 '13 at 6:28













16














My own solution is to use a templated holder class, as static members work fine in templates, and use this holder as a base class.



template <typename T>
struct static_holder

static T static_resource_;
;

template <typename T>
T static_holder<T>::static_resource_;


Now use the holder class:



class expensive_resource /*...*/ ;

class i_want_a_static_member : private static_holder<expensive_resource>

public:
void foo()

static_resource_.bar();

;


But as the name of the member is specified in the holder class, you can't use the same holder for more than one static member.






share|improve this answer


















  • 6





    Note: actually, you could use composition instead of inheritance. struct A static_holder<B> x; static_holder<B> y; ; would work, although it would not take advantage of Empty Base Optimization.

    – Matthieu M.
    Aug 13 '13 at 6:12















16














My own solution is to use a templated holder class, as static members work fine in templates, and use this holder as a base class.



template <typename T>
struct static_holder

static T static_resource_;
;

template <typename T>
T static_holder<T>::static_resource_;


Now use the holder class:



class expensive_resource /*...*/ ;

class i_want_a_static_member : private static_holder<expensive_resource>

public:
void foo()

static_resource_.bar();

;


But as the name of the member is specified in the holder class, you can't use the same holder for more than one static member.






share|improve this answer


















  • 6





    Note: actually, you could use composition instead of inheritance. struct A static_holder<B> x; static_holder<B> y; ; would work, although it would not take advantage of Empty Base Optimization.

    – Matthieu M.
    Aug 13 '13 at 6:12













16












16








16







My own solution is to use a templated holder class, as static members work fine in templates, and use this holder as a base class.



template <typename T>
struct static_holder

static T static_resource_;
;

template <typename T>
T static_holder<T>::static_resource_;


Now use the holder class:



class expensive_resource /*...*/ ;

class i_want_a_static_member : private static_holder<expensive_resource>

public:
void foo()

static_resource_.bar();

;


But as the name of the member is specified in the holder class, you can't use the same holder for more than one static member.






share|improve this answer













My own solution is to use a templated holder class, as static members work fine in templates, and use this holder as a base class.



template <typename T>
struct static_holder

static T static_resource_;
;

template <typename T>
T static_holder<T>::static_resource_;


Now use the holder class:



class expensive_resource /*...*/ ;

class i_want_a_static_member : private static_holder<expensive_resource>

public:
void foo()

static_resource_.bar();

;


But as the name of the member is specified in the holder class, you can't use the same holder for more than one static member.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 29 '12 at 14:06









peschepesche

1,62412434




1,62412434







  • 6





    Note: actually, you could use composition instead of inheritance. struct A static_holder<B> x; static_holder<B> y; ; would work, although it would not take advantage of Empty Base Optimization.

    – Matthieu M.
    Aug 13 '13 at 6:12












  • 6





    Note: actually, you could use composition instead of inheritance. struct A static_holder<B> x; static_holder<B> y; ; would work, although it would not take advantage of Empty Base Optimization.

    – Matthieu M.
    Aug 13 '13 at 6:12







6




6





Note: actually, you could use composition instead of inheritance. struct A static_holder<B> x; static_holder<B> y; ; would work, although it would not take advantage of Empty Base Optimization.

– Matthieu M.
Aug 13 '13 at 6:12





Note: actually, you could use composition instead of inheritance. struct A static_holder<B> x; static_holder<B> y; ; would work, although it would not take advantage of Empty Base Optimization.

– Matthieu M.
Aug 13 '13 at 6:12











3














As of C++ 17. You can now use inline variables to do this:



static const inline float foo = 1.25f;





share|improve this answer























  • On behalf of header purists everywhere, THANK YOU!

    – Syndog
    Dec 21 '18 at 19:23











  • Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable. See en.cppreference.com/w/cpp/language/inline. Also I would drop "const" from this answer as it's a special case.

    – ARA1307
    Dec 25 '18 at 2:58











  • Can you elaborate on where you get "Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable." from your cppreference link? I skimmed over it and didn't see where that came from.

    – redfeatherplusplus
    Jan 4 at 17:30












  • sorry, but I think that "upgrade to C++17" is not a viable way 99% of the time. Good point, though.

    – fiorentinoing
    Feb 8 at 12:55
















3














As of C++ 17. You can now use inline variables to do this:



static const inline float foo = 1.25f;





share|improve this answer























  • On behalf of header purists everywhere, THANK YOU!

    – Syndog
    Dec 21 '18 at 19:23











  • Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable. See en.cppreference.com/w/cpp/language/inline. Also I would drop "const" from this answer as it's a special case.

    – ARA1307
    Dec 25 '18 at 2:58











  • Can you elaborate on where you get "Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable." from your cppreference link? I skimmed over it and didn't see where that came from.

    – redfeatherplusplus
    Jan 4 at 17:30












  • sorry, but I think that "upgrade to C++17" is not a viable way 99% of the time. Good point, though.

    – fiorentinoing
    Feb 8 at 12:55














3












3








3







As of C++ 17. You can now use inline variables to do this:



static const inline float foo = 1.25f;





share|improve this answer













As of C++ 17. You can now use inline variables to do this:



static const inline float foo = 1.25f;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 22:22









redfeatherplusplusredfeatherplusplus

484




484












  • On behalf of header purists everywhere, THANK YOU!

    – Syndog
    Dec 21 '18 at 19:23











  • Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable. See en.cppreference.com/w/cpp/language/inline. Also I would drop "const" from this answer as it's a special case.

    – ARA1307
    Dec 25 '18 at 2:58











  • Can you elaborate on where you get "Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable." from your cppreference link? I skimmed over it and didn't see where that came from.

    – redfeatherplusplus
    Jan 4 at 17:30












  • sorry, but I think that "upgrade to C++17" is not a viable way 99% of the time. Good point, though.

    – fiorentinoing
    Feb 8 at 12:55


















  • On behalf of header purists everywhere, THANK YOU!

    – Syndog
    Dec 21 '18 at 19:23











  • Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable. See en.cppreference.com/w/cpp/language/inline. Also I would drop "const" from this answer as it's a special case.

    – ARA1307
    Dec 25 '18 at 2:58











  • Can you elaborate on where you get "Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable." from your cppreference link? I skimmed over it and didn't see where that came from.

    – redfeatherplusplus
    Jan 4 at 17:30












  • sorry, but I think that "upgrade to C++17" is not a viable way 99% of the time. Good point, though.

    – fiorentinoing
    Feb 8 at 12:55

















On behalf of header purists everywhere, THANK YOU!

– Syndog
Dec 21 '18 at 19:23





On behalf of header purists everywhere, THANK YOU!

– Syndog
Dec 21 '18 at 19:23













Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable. See en.cppreference.com/w/cpp/language/inline. Also I would drop "const" from this answer as it's a special case.

– ARA1307
Dec 25 '18 at 2:58





Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable. See en.cppreference.com/w/cpp/language/inline. Also I would drop "const" from this answer as it's a special case.

– ARA1307
Dec 25 '18 at 2:58













Can you elaborate on where you get "Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable." from your cppreference link? I skimmed over it and didn't see where that came from.

– redfeatherplusplus
Jan 4 at 17:30






Can you elaborate on where you get "Note "static inline" creates different variables in different unit translations. "inline" (without static) creates a "true" global variable." from your cppreference link? I skimmed over it and didn't see where that came from.

– redfeatherplusplus
Jan 4 at 17:30














sorry, but I think that "upgrade to C++17" is not a viable way 99% of the time. Good point, though.

– fiorentinoing
Feb 8 at 12:55






sorry, but I think that "upgrade to C++17" is not a viable way 99% of the time. Good point, though.

– fiorentinoing
Feb 8 at 12:55


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11709859%2fhow-to-have-static-data-members-in-a-header-only-library%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo