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.
c++ initialization-order
add a comment |
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.
c++ initialization-order
"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 wasconst float ge = foo();, wherefoois not a constexpr function, then it would no longer be guaranteed to be initialized beforeupperLimit. There are many other possibilities
– M.M
2 days ago
add a comment |
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.
c++ initialization-order
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
c++ initialization-order
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 wasconst float ge = foo();, wherefoois not a constexpr function, then it would no longer be guaranteed to be initialized beforeupperLimit. There are many other possibilities
– M.M
2 days ago
add a comment |
"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 wasconst float ge = foo();, wherefoois not a constexpr function, then it would no longer be guaranteed to be initialized beforeupperLimit. 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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
"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();, wherefoois not a constexpr function, then it would no longer be guaranteed to be initialized beforeupperLimit. There are many other possibilities– M.M
2 days ago