Global constants undefined in constructor










-1















I have two files for constants:



// constants.h
extern const std::string testString;

// constants.cpp
const std::string testString = "defined!";


I need to use this constant in the constructor of an object when the program is initialised, but it comes as undefined.
The code in the constructor is:



MyClass::MyClass() 
printf("Value of test string: %s", testString.c_str());


// output:
Value of test string: (null)


The class and the constants are defined in the same namespace, and it does not give me an error that the constant is undefined. After the object is initialised (e.g. with a hard-coded string), it works fine and prints out the value of the constant ("defined!"). Primitive constants seem to work fine in the constructor.



I think it has something to do with the constant not being initialised at that time (so from the .cpp file).
Do you know why this might happen? Does the initialisation of extern const occur after the program fully initialises?



Thank you in advance



EDIT:



Note that the string type is to simplify the issue, so converting it to char is not an option as I am also interested in having other non-primitive types too.



Code for a minimal dirty example of a program that shows this issue:



// constants.h
extern const std::string testString;

// constants.cpp
#include "constants.h"

const std::string testString = "defined!";

// MyClass.h
class MyClass

public:
MyClass();
virtual ~MyClass();
;

// MyClass.cpp
#include "MyClass.h"
#include "constants.h"

MyClass::MyClass()
printf("Value of test string: %sn", testString.c_str());


MyClass::~MyClass()

// main.cpp
#include "MyClass.h"
#include "constants.h"

MyClass my; // undefined string (outputs null)

int main(int argc, char** argv)
MyClass my; // defined string
return 0;



EDIT 2:



The solution in this case was to define an static inline function in the header file, as @Brian and @LightnessRacesinOrbit suggested. They both contributed to the final answer.



Here is the code:



inline std::string getTestString() return "defined!"; 


This allows to have non constexpr types as global constants.










share|improve this question
























  • Show the smallest program you can come up with that compiles, runs, and illustrates the problem. Unless, of course, in the process of reducing your actual code to the simplest example you see what's going wrong.

    – Pete Becker
    Nov 14 '18 at 18:13











  • @PeteBecker This question was for me to understand why it happens, not to have somebody fix my code, so I only added the relevant parts. Regardless, I added the code if you'd like to try it.

    – Colmike
    Nov 15 '18 at 11:00











  • "Why it happens" often depends on "what it's doing"; people can make guesses from code fragments, but can't give definite answers. It's quite common to see code fragments that have nothing wrong; the programmer didn't know enough about the problem to identify the critical parts. The other thing is, in reducing code to the smallest example that shows the problem, you'll often see it for yourself. That's an important skill.

    – Pete Becker
    Nov 15 '18 at 13:55















-1















I have two files for constants:



// constants.h
extern const std::string testString;

// constants.cpp
const std::string testString = "defined!";


I need to use this constant in the constructor of an object when the program is initialised, but it comes as undefined.
The code in the constructor is:



MyClass::MyClass() 
printf("Value of test string: %s", testString.c_str());


// output:
Value of test string: (null)


The class and the constants are defined in the same namespace, and it does not give me an error that the constant is undefined. After the object is initialised (e.g. with a hard-coded string), it works fine and prints out the value of the constant ("defined!"). Primitive constants seem to work fine in the constructor.



I think it has something to do with the constant not being initialised at that time (so from the .cpp file).
Do you know why this might happen? Does the initialisation of extern const occur after the program fully initialises?



Thank you in advance



EDIT:



Note that the string type is to simplify the issue, so converting it to char is not an option as I am also interested in having other non-primitive types too.



Code for a minimal dirty example of a program that shows this issue:



// constants.h
extern const std::string testString;

// constants.cpp
#include "constants.h"

const std::string testString = "defined!";

// MyClass.h
class MyClass

public:
MyClass();
virtual ~MyClass();
;

// MyClass.cpp
#include "MyClass.h"
#include "constants.h"

MyClass::MyClass()
printf("Value of test string: %sn", testString.c_str());


MyClass::~MyClass()

// main.cpp
#include "MyClass.h"
#include "constants.h"

MyClass my; // undefined string (outputs null)

int main(int argc, char** argv)
MyClass my; // defined string
return 0;



EDIT 2:



The solution in this case was to define an static inline function in the header file, as @Brian and @LightnessRacesinOrbit suggested. They both contributed to the final answer.



Here is the code:



inline std::string getTestString() return "defined!"; 


This allows to have non constexpr types as global constants.










share|improve this question
























  • Show the smallest program you can come up with that compiles, runs, and illustrates the problem. Unless, of course, in the process of reducing your actual code to the simplest example you see what's going wrong.

    – Pete Becker
    Nov 14 '18 at 18:13











  • @PeteBecker This question was for me to understand why it happens, not to have somebody fix my code, so I only added the relevant parts. Regardless, I added the code if you'd like to try it.

    – Colmike
    Nov 15 '18 at 11:00











  • "Why it happens" often depends on "what it's doing"; people can make guesses from code fragments, but can't give definite answers. It's quite common to see code fragments that have nothing wrong; the programmer didn't know enough about the problem to identify the critical parts. The other thing is, in reducing code to the smallest example that shows the problem, you'll often see it for yourself. That's an important skill.

    – Pete Becker
    Nov 15 '18 at 13:55













-1












-1








-1








I have two files for constants:



// constants.h
extern const std::string testString;

// constants.cpp
const std::string testString = "defined!";


I need to use this constant in the constructor of an object when the program is initialised, but it comes as undefined.
The code in the constructor is:



MyClass::MyClass() 
printf("Value of test string: %s", testString.c_str());


// output:
Value of test string: (null)


The class and the constants are defined in the same namespace, and it does not give me an error that the constant is undefined. After the object is initialised (e.g. with a hard-coded string), it works fine and prints out the value of the constant ("defined!"). Primitive constants seem to work fine in the constructor.



I think it has something to do with the constant not being initialised at that time (so from the .cpp file).
Do you know why this might happen? Does the initialisation of extern const occur after the program fully initialises?



Thank you in advance



EDIT:



Note that the string type is to simplify the issue, so converting it to char is not an option as I am also interested in having other non-primitive types too.



Code for a minimal dirty example of a program that shows this issue:



// constants.h
extern const std::string testString;

// constants.cpp
#include "constants.h"

const std::string testString = "defined!";

// MyClass.h
class MyClass

public:
MyClass();
virtual ~MyClass();
;

// MyClass.cpp
#include "MyClass.h"
#include "constants.h"

MyClass::MyClass()
printf("Value of test string: %sn", testString.c_str());


MyClass::~MyClass()

// main.cpp
#include "MyClass.h"
#include "constants.h"

MyClass my; // undefined string (outputs null)

int main(int argc, char** argv)
MyClass my; // defined string
return 0;



EDIT 2:



The solution in this case was to define an static inline function in the header file, as @Brian and @LightnessRacesinOrbit suggested. They both contributed to the final answer.



Here is the code:



inline std::string getTestString() return "defined!"; 


This allows to have non constexpr types as global constants.










share|improve this question
















I have two files for constants:



// constants.h
extern const std::string testString;

// constants.cpp
const std::string testString = "defined!";


I need to use this constant in the constructor of an object when the program is initialised, but it comes as undefined.
The code in the constructor is:



MyClass::MyClass() 
printf("Value of test string: %s", testString.c_str());


// output:
Value of test string: (null)


The class and the constants are defined in the same namespace, and it does not give me an error that the constant is undefined. After the object is initialised (e.g. with a hard-coded string), it works fine and prints out the value of the constant ("defined!"). Primitive constants seem to work fine in the constructor.



I think it has something to do with the constant not being initialised at that time (so from the .cpp file).
Do you know why this might happen? Does the initialisation of extern const occur after the program fully initialises?



Thank you in advance



EDIT:



Note that the string type is to simplify the issue, so converting it to char is not an option as I am also interested in having other non-primitive types too.



Code for a minimal dirty example of a program that shows this issue:



// constants.h
extern const std::string testString;

// constants.cpp
#include "constants.h"

const std::string testString = "defined!";

// MyClass.h
class MyClass

public:
MyClass();
virtual ~MyClass();
;

// MyClass.cpp
#include "MyClass.h"
#include "constants.h"

MyClass::MyClass()
printf("Value of test string: %sn", testString.c_str());


MyClass::~MyClass()

// main.cpp
#include "MyClass.h"
#include "constants.h"

MyClass my; // undefined string (outputs null)

int main(int argc, char** argv)
MyClass my; // defined string
return 0;



EDIT 2:



The solution in this case was to define an static inline function in the header file, as @Brian and @LightnessRacesinOrbit suggested. They both contributed to the final answer.



Here is the code:



inline std::string getTestString() return "defined!"; 


This allows to have non constexpr types as global constants.







c++ header-files






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:49







Colmike

















asked Nov 14 '18 at 18:11









ColmikeColmike

756




756












  • Show the smallest program you can come up with that compiles, runs, and illustrates the problem. Unless, of course, in the process of reducing your actual code to the simplest example you see what's going wrong.

    – Pete Becker
    Nov 14 '18 at 18:13











  • @PeteBecker This question was for me to understand why it happens, not to have somebody fix my code, so I only added the relevant parts. Regardless, I added the code if you'd like to try it.

    – Colmike
    Nov 15 '18 at 11:00











  • "Why it happens" often depends on "what it's doing"; people can make guesses from code fragments, but can't give definite answers. It's quite common to see code fragments that have nothing wrong; the programmer didn't know enough about the problem to identify the critical parts. The other thing is, in reducing code to the smallest example that shows the problem, you'll often see it for yourself. That's an important skill.

    – Pete Becker
    Nov 15 '18 at 13:55

















  • Show the smallest program you can come up with that compiles, runs, and illustrates the problem. Unless, of course, in the process of reducing your actual code to the simplest example you see what's going wrong.

    – Pete Becker
    Nov 14 '18 at 18:13











  • @PeteBecker This question was for me to understand why it happens, not to have somebody fix my code, so I only added the relevant parts. Regardless, I added the code if you'd like to try it.

    – Colmike
    Nov 15 '18 at 11:00











  • "Why it happens" often depends on "what it's doing"; people can make guesses from code fragments, but can't give definite answers. It's quite common to see code fragments that have nothing wrong; the programmer didn't know enough about the problem to identify the critical parts. The other thing is, in reducing code to the smallest example that shows the problem, you'll often see it for yourself. That's an important skill.

    – Pete Becker
    Nov 15 '18 at 13:55
















Show the smallest program you can come up with that compiles, runs, and illustrates the problem. Unless, of course, in the process of reducing your actual code to the simplest example you see what's going wrong.

– Pete Becker
Nov 14 '18 at 18:13





Show the smallest program you can come up with that compiles, runs, and illustrates the problem. Unless, of course, in the process of reducing your actual code to the simplest example you see what's going wrong.

– Pete Becker
Nov 14 '18 at 18:13













@PeteBecker This question was for me to understand why it happens, not to have somebody fix my code, so I only added the relevant parts. Regardless, I added the code if you'd like to try it.

– Colmike
Nov 15 '18 at 11:00





@PeteBecker This question was for me to understand why it happens, not to have somebody fix my code, so I only added the relevant parts. Regardless, I added the code if you'd like to try it.

– Colmike
Nov 15 '18 at 11:00













"Why it happens" often depends on "what it's doing"; people can make guesses from code fragments, but can't give definite answers. It's quite common to see code fragments that have nothing wrong; the programmer didn't know enough about the problem to identify the critical parts. The other thing is, in reducing code to the smallest example that shows the problem, you'll often see it for yourself. That's an important skill.

– Pete Becker
Nov 15 '18 at 13:55





"Why it happens" often depends on "what it's doing"; people can make guesses from code fragments, but can't give definite answers. It's quite common to see code fragments that have nothing wrong; the programmer didn't know enough about the problem to identify the critical parts. The other thing is, in reducing code to the smallest example that shows the problem, you'll often see it for yourself. That's an important skill.

– Pete Becker
Nov 15 '18 at 13:55












1 Answer
1






active

oldest

votes


















5














Within the constants.cpp translation unit, testString will be initialized before any subsequently defined non-local variable. Between translation units, we have what's called the "static initialization order fiasco"; any translation unit other than constants.cpp cannot assume that testString has been initialized until after main has started executing, so if it tries to read its value during one of its own non-local initializations, then it may observe a zero-initialized std::string object, which has undefined behaviour.



My suggestion to avoid this problem, which is also the rule followed at my former workplace, is that if you must have global constants, make them constexpr if possible, and be very wary of having any non-constexpr global variables. std::string isn't constexpr (yet), but an old-fashioned char array could work:



// constants.h
inline constexpr char testString = "defined!";

// constants.cpp
// no need to define `testString` here!





share|improve this answer

























  • Hi, I thought it had something to do with the order of initialisation, so thanks for confirming it. However, I think the inline keyword is C++17 and I am unfortunately on C++14 for this issue (correct me if wrong). I will keep looking at what to do. Thanks again!

    – Colmike
    Nov 15 '18 at 10:23






  • 1





    @Colmike Function-statics can be useful. Failing that, avoid globals!

    – Lightness Races in Orbit
    Nov 15 '18 at 11:13











  • @LightnessRacesinOrbit Thanks for that! That helped solving the issue. I used inline functions in the header file and it prints the defined value. This seems to be a way to handle non constexpr types (e.g. string) directly. I will have to check how feasible this is in the long run for many application-wide globals though

    – Colmike
    Nov 15 '18 at 11:29












  • @Colmike Nice one! Right on all counts. I do agree you should think hard about whether it will scale for you but it works in a pinch. Ideally I think you would leave the constants where they are and fix my being a global but it's not always so easy.

    – Lightness Races in Orbit
    Nov 15 '18 at 11:35












  • @LightnessRacesinOrbit Making my not global would also fix this issue, but in my case, this is one main.cpp of many (the application is a cross-platform library) and I only have access to the Linux main. I do not know if changing this is viable for other teams that may have a different API. Thanks for that, I will check.

    – Colmike
    Nov 15 '18 at 11:56











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%2f53306402%2fglobal-constants-undefined-in-constructor%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Within the constants.cpp translation unit, testString will be initialized before any subsequently defined non-local variable. Between translation units, we have what's called the "static initialization order fiasco"; any translation unit other than constants.cpp cannot assume that testString has been initialized until after main has started executing, so if it tries to read its value during one of its own non-local initializations, then it may observe a zero-initialized std::string object, which has undefined behaviour.



My suggestion to avoid this problem, which is also the rule followed at my former workplace, is that if you must have global constants, make them constexpr if possible, and be very wary of having any non-constexpr global variables. std::string isn't constexpr (yet), but an old-fashioned char array could work:



// constants.h
inline constexpr char testString = "defined!";

// constants.cpp
// no need to define `testString` here!





share|improve this answer

























  • Hi, I thought it had something to do with the order of initialisation, so thanks for confirming it. However, I think the inline keyword is C++17 and I am unfortunately on C++14 for this issue (correct me if wrong). I will keep looking at what to do. Thanks again!

    – Colmike
    Nov 15 '18 at 10:23






  • 1





    @Colmike Function-statics can be useful. Failing that, avoid globals!

    – Lightness Races in Orbit
    Nov 15 '18 at 11:13











  • @LightnessRacesinOrbit Thanks for that! That helped solving the issue. I used inline functions in the header file and it prints the defined value. This seems to be a way to handle non constexpr types (e.g. string) directly. I will have to check how feasible this is in the long run for many application-wide globals though

    – Colmike
    Nov 15 '18 at 11:29












  • @Colmike Nice one! Right on all counts. I do agree you should think hard about whether it will scale for you but it works in a pinch. Ideally I think you would leave the constants where they are and fix my being a global but it's not always so easy.

    – Lightness Races in Orbit
    Nov 15 '18 at 11:35












  • @LightnessRacesinOrbit Making my not global would also fix this issue, but in my case, this is one main.cpp of many (the application is a cross-platform library) and I only have access to the Linux main. I do not know if changing this is viable for other teams that may have a different API. Thanks for that, I will check.

    – Colmike
    Nov 15 '18 at 11:56
















5














Within the constants.cpp translation unit, testString will be initialized before any subsequently defined non-local variable. Between translation units, we have what's called the "static initialization order fiasco"; any translation unit other than constants.cpp cannot assume that testString has been initialized until after main has started executing, so if it tries to read its value during one of its own non-local initializations, then it may observe a zero-initialized std::string object, which has undefined behaviour.



My suggestion to avoid this problem, which is also the rule followed at my former workplace, is that if you must have global constants, make them constexpr if possible, and be very wary of having any non-constexpr global variables. std::string isn't constexpr (yet), but an old-fashioned char array could work:



// constants.h
inline constexpr char testString = "defined!";

// constants.cpp
// no need to define `testString` here!





share|improve this answer

























  • Hi, I thought it had something to do with the order of initialisation, so thanks for confirming it. However, I think the inline keyword is C++17 and I am unfortunately on C++14 for this issue (correct me if wrong). I will keep looking at what to do. Thanks again!

    – Colmike
    Nov 15 '18 at 10:23






  • 1





    @Colmike Function-statics can be useful. Failing that, avoid globals!

    – Lightness Races in Orbit
    Nov 15 '18 at 11:13











  • @LightnessRacesinOrbit Thanks for that! That helped solving the issue. I used inline functions in the header file and it prints the defined value. This seems to be a way to handle non constexpr types (e.g. string) directly. I will have to check how feasible this is in the long run for many application-wide globals though

    – Colmike
    Nov 15 '18 at 11:29












  • @Colmike Nice one! Right on all counts. I do agree you should think hard about whether it will scale for you but it works in a pinch. Ideally I think you would leave the constants where they are and fix my being a global but it's not always so easy.

    – Lightness Races in Orbit
    Nov 15 '18 at 11:35












  • @LightnessRacesinOrbit Making my not global would also fix this issue, but in my case, this is one main.cpp of many (the application is a cross-platform library) and I only have access to the Linux main. I do not know if changing this is viable for other teams that may have a different API. Thanks for that, I will check.

    – Colmike
    Nov 15 '18 at 11:56














5












5








5







Within the constants.cpp translation unit, testString will be initialized before any subsequently defined non-local variable. Between translation units, we have what's called the "static initialization order fiasco"; any translation unit other than constants.cpp cannot assume that testString has been initialized until after main has started executing, so if it tries to read its value during one of its own non-local initializations, then it may observe a zero-initialized std::string object, which has undefined behaviour.



My suggestion to avoid this problem, which is also the rule followed at my former workplace, is that if you must have global constants, make them constexpr if possible, and be very wary of having any non-constexpr global variables. std::string isn't constexpr (yet), but an old-fashioned char array could work:



// constants.h
inline constexpr char testString = "defined!";

// constants.cpp
// no need to define `testString` here!





share|improve this answer















Within the constants.cpp translation unit, testString will be initialized before any subsequently defined non-local variable. Between translation units, we have what's called the "static initialization order fiasco"; any translation unit other than constants.cpp cannot assume that testString has been initialized until after main has started executing, so if it tries to read its value during one of its own non-local initializations, then it may observe a zero-initialized std::string object, which has undefined behaviour.



My suggestion to avoid this problem, which is also the rule followed at my former workplace, is that if you must have global constants, make them constexpr if possible, and be very wary of having any non-constexpr global variables. std::string isn't constexpr (yet), but an old-fashioned char array could work:



// constants.h
inline constexpr char testString = "defined!";

// constants.cpp
// no need to define `testString` here!






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 18:24

























answered Nov 14 '18 at 18:15









BrianBrian

65.1k796183




65.1k796183












  • Hi, I thought it had something to do with the order of initialisation, so thanks for confirming it. However, I think the inline keyword is C++17 and I am unfortunately on C++14 for this issue (correct me if wrong). I will keep looking at what to do. Thanks again!

    – Colmike
    Nov 15 '18 at 10:23






  • 1





    @Colmike Function-statics can be useful. Failing that, avoid globals!

    – Lightness Races in Orbit
    Nov 15 '18 at 11:13











  • @LightnessRacesinOrbit Thanks for that! That helped solving the issue. I used inline functions in the header file and it prints the defined value. This seems to be a way to handle non constexpr types (e.g. string) directly. I will have to check how feasible this is in the long run for many application-wide globals though

    – Colmike
    Nov 15 '18 at 11:29












  • @Colmike Nice one! Right on all counts. I do agree you should think hard about whether it will scale for you but it works in a pinch. Ideally I think you would leave the constants where they are and fix my being a global but it's not always so easy.

    – Lightness Races in Orbit
    Nov 15 '18 at 11:35












  • @LightnessRacesinOrbit Making my not global would also fix this issue, but in my case, this is one main.cpp of many (the application is a cross-platform library) and I only have access to the Linux main. I do not know if changing this is viable for other teams that may have a different API. Thanks for that, I will check.

    – Colmike
    Nov 15 '18 at 11:56


















  • Hi, I thought it had something to do with the order of initialisation, so thanks for confirming it. However, I think the inline keyword is C++17 and I am unfortunately on C++14 for this issue (correct me if wrong). I will keep looking at what to do. Thanks again!

    – Colmike
    Nov 15 '18 at 10:23






  • 1





    @Colmike Function-statics can be useful. Failing that, avoid globals!

    – Lightness Races in Orbit
    Nov 15 '18 at 11:13











  • @LightnessRacesinOrbit Thanks for that! That helped solving the issue. I used inline functions in the header file and it prints the defined value. This seems to be a way to handle non constexpr types (e.g. string) directly. I will have to check how feasible this is in the long run for many application-wide globals though

    – Colmike
    Nov 15 '18 at 11:29












  • @Colmike Nice one! Right on all counts. I do agree you should think hard about whether it will scale for you but it works in a pinch. Ideally I think you would leave the constants where they are and fix my being a global but it's not always so easy.

    – Lightness Races in Orbit
    Nov 15 '18 at 11:35












  • @LightnessRacesinOrbit Making my not global would also fix this issue, but in my case, this is one main.cpp of many (the application is a cross-platform library) and I only have access to the Linux main. I do not know if changing this is viable for other teams that may have a different API. Thanks for that, I will check.

    – Colmike
    Nov 15 '18 at 11:56

















Hi, I thought it had something to do with the order of initialisation, so thanks for confirming it. However, I think the inline keyword is C++17 and I am unfortunately on C++14 for this issue (correct me if wrong). I will keep looking at what to do. Thanks again!

– Colmike
Nov 15 '18 at 10:23





Hi, I thought it had something to do with the order of initialisation, so thanks for confirming it. However, I think the inline keyword is C++17 and I am unfortunately on C++14 for this issue (correct me if wrong). I will keep looking at what to do. Thanks again!

– Colmike
Nov 15 '18 at 10:23




1




1





@Colmike Function-statics can be useful. Failing that, avoid globals!

– Lightness Races in Orbit
Nov 15 '18 at 11:13





@Colmike Function-statics can be useful. Failing that, avoid globals!

– Lightness Races in Orbit
Nov 15 '18 at 11:13













@LightnessRacesinOrbit Thanks for that! That helped solving the issue. I used inline functions in the header file and it prints the defined value. This seems to be a way to handle non constexpr types (e.g. string) directly. I will have to check how feasible this is in the long run for many application-wide globals though

– Colmike
Nov 15 '18 at 11:29






@LightnessRacesinOrbit Thanks for that! That helped solving the issue. I used inline functions in the header file and it prints the defined value. This seems to be a way to handle non constexpr types (e.g. string) directly. I will have to check how feasible this is in the long run for many application-wide globals though

– Colmike
Nov 15 '18 at 11:29














@Colmike Nice one! Right on all counts. I do agree you should think hard about whether it will scale for you but it works in a pinch. Ideally I think you would leave the constants where they are and fix my being a global but it's not always so easy.

– Lightness Races in Orbit
Nov 15 '18 at 11:35






@Colmike Nice one! Right on all counts. I do agree you should think hard about whether it will scale for you but it works in a pinch. Ideally I think you would leave the constants where they are and fix my being a global but it's not always so easy.

– Lightness Races in Orbit
Nov 15 '18 at 11:35














@LightnessRacesinOrbit Making my not global would also fix this issue, but in my case, this is one main.cpp of many (the application is a cross-platform library) and I only have access to the Linux main. I do not know if changing this is viable for other teams that may have a different API. Thanks for that, I will check.

– Colmike
Nov 15 '18 at 11:56






@LightnessRacesinOrbit Making my not global would also fix this issue, but in my case, this is one main.cpp of many (the application is a cross-platform library) and I only have access to the Linux main. I do not know if changing this is viable for other teams that may have a different API. Thanks for that, I will check.

– Colmike
Nov 15 '18 at 11:56




















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%2f53306402%2fglobal-constants-undefined-in-constructor%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto