In C++, given namespace-wide const float dependant on a global const float, is their initialization order...











up vote
1
down vote

favorite












If there is a pair of .h/.cpp files with something like, respectively,



extern const float ge;


and



const float ge = 2.2f;


in them, and the other .cpp file with something like



namespace {
const float upperLimit = 2.0f * ge;
} // namespace

bool foo(float a)
{
return a < upperLimit;
}


in it, is it guaranteed that upperLimit is initialized after the ge?



I'm aware about indefinite initialization order of global objects from different translation units. I'd like to be sure if it is true for the case of mixed global and namespace-wide objects.










share|improve this question






















  • "something like" is pretty vague, it would be clearer if you said that that was exactly what you are asking about
    – M.M
    2 days ago










  • There are just more than one global floats, that's it. Or do you think their exact values matter?
    – Victor Che
    2 days ago






  • 1




    If the code was const float ge = foo(); , where foo is not a constexpr function, then it would no longer be guaranteed to be initialized before upperLimit. There are many other possibilities
    – M.M
    2 days ago

















up vote
1
down vote

favorite












If there is a pair of .h/.cpp files with something like, respectively,



extern const float ge;


and



const float ge = 2.2f;


in them, and the other .cpp file with something like



namespace {
const float upperLimit = 2.0f * ge;
} // namespace

bool foo(float a)
{
return a < upperLimit;
}


in it, is it guaranteed that upperLimit is initialized after the ge?



I'm aware about indefinite initialization order of global objects from different translation units. I'd like to be sure if it is true for the case of mixed global and namespace-wide objects.










share|improve this question






















  • "something like" is pretty vague, it would be clearer if you said that that was exactly what you are asking about
    – M.M
    2 days ago










  • There are just more than one global floats, that's it. Or do you think their exact values matter?
    – Victor Che
    2 days ago






  • 1




    If the code was const float ge = foo(); , where foo is not a constexpr function, then it would no longer be guaranteed to be initialized before upperLimit. There are many other possibilities
    – M.M
    2 days ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











If there is a pair of .h/.cpp files with something like, respectively,



extern const float ge;


and



const float ge = 2.2f;


in them, and the other .cpp file with something like



namespace {
const float upperLimit = 2.0f * ge;
} // namespace

bool foo(float a)
{
return a < upperLimit;
}


in it, is it guaranteed that upperLimit is initialized after the ge?



I'm aware about indefinite initialization order of global objects from different translation units. I'd like to be sure if it is true for the case of mixed global and namespace-wide objects.










share|improve this question













If there is a pair of .h/.cpp files with something like, respectively,



extern const float ge;


and



const float ge = 2.2f;


in them, and the other .cpp file with something like



namespace {
const float upperLimit = 2.0f * ge;
} // namespace

bool foo(float a)
{
return a < upperLimit;
}


in it, is it guaranteed that upperLimit is initialized after the ge?



I'm aware about indefinite initialization order of global objects from different translation units. I'd like to be sure if it is true for the case of mixed global and namespace-wide objects.







c++ initialization-order






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Victor Che

285




285












  • "something like" is pretty vague, it would be clearer if you said that that was exactly what you are asking about
    – M.M
    2 days ago










  • There are just more than one global floats, that's it. Or do you think their exact values matter?
    – Victor Che
    2 days ago






  • 1




    If the code was const float ge = foo(); , where foo is not a constexpr function, then it would no longer be guaranteed to be initialized before upperLimit. There are many other possibilities
    – M.M
    2 days ago




















  • "something like" is pretty vague, it would be clearer if you said that that was exactly what you are asking about
    – M.M
    2 days ago










  • There are just more than one global floats, that's it. Or do you think their exact values matter?
    – Victor Che
    2 days ago






  • 1




    If the code was const float ge = foo(); , where foo is not a constexpr function, then it would no longer be guaranteed to be initialized before upperLimit. There are many other possibilities
    – M.M
    2 days ago


















"something like" is pretty vague, it would be clearer if you said that that was exactly what you are asking about
– M.M
2 days ago




"something like" is pretty vague, it would be clearer if you said that that was exactly what you are asking about
– M.M
2 days ago












There are just more than one global floats, that's it. Or do you think their exact values matter?
– Victor Che
2 days ago




There are just more than one global floats, that's it. Or do you think their exact values matter?
– Victor Che
2 days ago




1




1




If the code was const float ge = foo(); , where foo is not a constexpr function, then it would no longer be guaranteed to be initialized before upperLimit. There are many other possibilities
– M.M
2 days ago






If the code was const float ge = foo(); , where foo is not a constexpr function, then it would no longer be guaranteed to be initialized before upperLimit. There are many other possibilities
– M.M
2 days ago














1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










In your code, ge is guaranteed to be initialized before upperLimit but this is nothing to do with namespaces. The case would be the same if you didn't have the namespace. Namespaces have no effect on initialization order.



The code const float ge = 2.2f; is part of constant initialization, which is part of static initialization, because it is a variable with static storage duration being initialized by a constant expression.



However, in the code const float upperLimit = 2.0f * ge;, the initializer is NOT a constant expression because the value of ge is not known. So it is not static initialization (and therefore falls under dynamic initialization).



All static initialization strongly happens before dynamic initialization (C++17 [basic.start.static]/2) so the code is correct.






share|improve this answer





















  • As far as I know float initialization cannot be static because float representation is platform-depenent so it must be initialized dynamically, correct me if i'm wrong.
    – Victor Che
    2 days ago










  • @VictorChe I linked to the cppreference page already. Floating point constants are constant expressions, see here also
    – M.M
    2 days ago










  • Thank you so much.
    – Victor Che
    2 days ago











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',
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%2f53203556%2fin-c-given-namespace-wide-const-float-dependant-on-a-global-const-float-is-t%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










In your code, ge is guaranteed to be initialized before upperLimit but this is nothing to do with namespaces. The case would be the same if you didn't have the namespace. Namespaces have no effect on initialization order.



The code const float ge = 2.2f; is part of constant initialization, which is part of static initialization, because it is a variable with static storage duration being initialized by a constant expression.



However, in the code const float upperLimit = 2.0f * ge;, the initializer is NOT a constant expression because the value of ge is not known. So it is not static initialization (and therefore falls under dynamic initialization).



All static initialization strongly happens before dynamic initialization (C++17 [basic.start.static]/2) so the code is correct.






share|improve this answer





















  • As far as I know float initialization cannot be static because float representation is platform-depenent so it must be initialized dynamically, correct me if i'm wrong.
    – Victor Che
    2 days ago










  • @VictorChe I linked to the cppreference page already. Floating point constants are constant expressions, see here also
    – M.M
    2 days ago










  • Thank you so much.
    – Victor Che
    2 days ago















up vote
3
down vote



accepted










In your code, ge is guaranteed to be initialized before upperLimit but this is nothing to do with namespaces. The case would be the same if you didn't have the namespace. Namespaces have no effect on initialization order.



The code const float ge = 2.2f; is part of constant initialization, which is part of static initialization, because it is a variable with static storage duration being initialized by a constant expression.



However, in the code const float upperLimit = 2.0f * ge;, the initializer is NOT a constant expression because the value of ge is not known. So it is not static initialization (and therefore falls under dynamic initialization).



All static initialization strongly happens before dynamic initialization (C++17 [basic.start.static]/2) so the code is correct.






share|improve this answer





















  • As far as I know float initialization cannot be static because float representation is platform-depenent so it must be initialized dynamically, correct me if i'm wrong.
    – Victor Che
    2 days ago










  • @VictorChe I linked to the cppreference page already. Floating point constants are constant expressions, see here also
    – M.M
    2 days ago










  • Thank you so much.
    – Victor Che
    2 days ago













up vote
3
down vote



accepted







up vote
3
down vote



accepted






In your code, ge is guaranteed to be initialized before upperLimit but this is nothing to do with namespaces. The case would be the same if you didn't have the namespace. Namespaces have no effect on initialization order.



The code const float ge = 2.2f; is part of constant initialization, which is part of static initialization, because it is a variable with static storage duration being initialized by a constant expression.



However, in the code const float upperLimit = 2.0f * ge;, the initializer is NOT a constant expression because the value of ge is not known. So it is not static initialization (and therefore falls under dynamic initialization).



All static initialization strongly happens before dynamic initialization (C++17 [basic.start.static]/2) so the code is correct.






share|improve this answer












In your code, ge is guaranteed to be initialized before upperLimit but this is nothing to do with namespaces. The case would be the same if you didn't have the namespace. Namespaces have no effect on initialization order.



The code const float ge = 2.2f; is part of constant initialization, which is part of static initialization, because it is a variable with static storage duration being initialized by a constant expression.



However, in the code const float upperLimit = 2.0f * ge;, the initializer is NOT a constant expression because the value of ge is not known. So it is not static initialization (and therefore falls under dynamic initialization).



All static initialization strongly happens before dynamic initialization (C++17 [basic.start.static]/2) so the code is correct.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









M.M

103k11108226




103k11108226












  • As far as I know float initialization cannot be static because float representation is platform-depenent so it must be initialized dynamically, correct me if i'm wrong.
    – Victor Che
    2 days ago










  • @VictorChe I linked to the cppreference page already. Floating point constants are constant expressions, see here also
    – M.M
    2 days ago










  • Thank you so much.
    – Victor Che
    2 days ago


















  • As far as I know float initialization cannot be static because float representation is platform-depenent so it must be initialized dynamically, correct me if i'm wrong.
    – Victor Che
    2 days ago










  • @VictorChe I linked to the cppreference page already. Floating point constants are constant expressions, see here also
    – M.M
    2 days ago










  • Thank you so much.
    – Victor Che
    2 days ago
















As far as I know float initialization cannot be static because float representation is platform-depenent so it must be initialized dynamically, correct me if i'm wrong.
– Victor Che
2 days ago




As far as I know float initialization cannot be static because float representation is platform-depenent so it must be initialized dynamically, correct me if i'm wrong.
– Victor Che
2 days ago












@VictorChe I linked to the cppreference page already. Floating point constants are constant expressions, see here also
– M.M
2 days ago




@VictorChe I linked to the cppreference page already. Floating point constants are constant expressions, see here also
– M.M
2 days ago












Thank you so much.
– Victor Che
2 days ago




Thank you so much.
– Victor Che
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203556%2fin-c-given-namespace-wide-const-float-dependant-on-a-global-const-float-is-t%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Landwehr

Reims

Javascript gets undefined on array