Usage before initialization of const member, is this expected bahviour of gcc and clang? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to initialize private static members in C++?Clang vs GCC - which produces better binaries?Why does not g++ or clang elicit a warning when truncating a non-const variable by assigning it to a variable of smaller type?Switching between GCC and Clang/LLVM using CMakeClang vs GCC for my Linux Development projectWarnings to determine unsafe casts clangComparing two map::iterators: why does it need the copy constructor of std::pair?Defining a std::map with an undefined comparison function compiles and links, which surprises me`auto x = type…` initialization syntax and `explicit` conversion operator - clang vs gccWarning for a cast from a char literal to a char *
Why is it faster to reheat something than it is to cook it?
What was the first language to use conditional keywords?
An adverb for when you're not exaggerating
What are the diatonic extended chords of C major?
Is it a good idea to use CNN to classify 1D signal?
Effects on objects due to a brief relocation of massive amounts of mass
How do living politicians protect their readily obtainable signatures from misuse?
Why weren't discrete x86 CPUs ever used in game hardware?
What does it mean that physics no longer uses mechanical models to describe phenomena?
Do I really need to have a message in a novel to appeal to readers?
Is it fair for a professor to grade us on the possession of past papers?
Significance of Cersei's obsession with elephants?
Is there any word for a place full of confusion?
Can a new player join a group only when a new campaign starts?
Did Deadpool rescue all of the X-Force?
How to compare two different files line by line in unix?
Illegal assignment from sObject to Id
Why is Nikon 1.4g better when Nikon 1.8g is sharper?
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
How does the math work when buying airline miles?
Selecting user stories during sprint planning
Why do early math courses focus on the cross sections of a cone and not on other 3D objects?
Is there hard evidence that the grant peer review system performs significantly better than random?
Using audio cues to encourage good posture
Usage before initialization of const member, is this expected bahviour of gcc and clang?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to initialize private static members in C++?Clang vs GCC - which produces better binaries?Why does not g++ or clang elicit a warning when truncating a non-const variable by assigning it to a variable of smaller type?Switching between GCC and Clang/LLVM using CMakeClang vs GCC for my Linux Development projectWarnings to determine unsafe casts clangComparing two map::iterators: why does it need the copy constructor of std::pair?Defining a std::map with an undefined comparison function compiles and links, which surprises me`auto x = type…` initialization syntax and `explicit` conversion operator - clang vs gccWarning for a cast from a char literal to a char *
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Consider the following snippet. Class test
has a const member a
and a member function fun
which returns a
. An initialization list is used to to initialize a
in the constructor. However in the initialization list a lambda is used to initialize a
with the returned value of fun. This leads to different behaviors of clang and gcc at compile and runtime, depending on the optimization level. Below the snippet and the different outputs at compile and runtime are listed. Is this expected behavior of gcc and clang?
#include <iostream>
class test
public:
const int a;
test(): a([this]()return fun();())
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Compiletime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
3 warnings generated.
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
g++ -std=c++17 -Wall -Wextra -Wpedantic
No output
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
lambda_in_initializer_list.cpp: In function ‘int main()’:
lambda_in_initializer_list.cpp:18:20: warning: ‘t.test::a’ is used uninitialized in this function [-Wuninitialized]
std::cout << t.a << 'n';
~~^
Runtime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
0
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
4196112
g++ -std=c++17 -Wall -Wextra -Wpedantic
Non deterministic output.
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
0
c++ gcc clang compiler-warnings
add a comment |
Consider the following snippet. Class test
has a const member a
and a member function fun
which returns a
. An initialization list is used to to initialize a
in the constructor. However in the initialization list a lambda is used to initialize a
with the returned value of fun. This leads to different behaviors of clang and gcc at compile and runtime, depending on the optimization level. Below the snippet and the different outputs at compile and runtime are listed. Is this expected behavior of gcc and clang?
#include <iostream>
class test
public:
const int a;
test(): a([this]()return fun();())
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Compiletime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
3 warnings generated.
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
g++ -std=c++17 -Wall -Wextra -Wpedantic
No output
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
lambda_in_initializer_list.cpp: In function ‘int main()’:
lambda_in_initializer_list.cpp:18:20: warning: ‘t.test::a’ is used uninitialized in this function [-Wuninitialized]
std::cout << t.a << 'n';
~~^
Runtime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
0
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
4196112
g++ -std=c++17 -Wall -Wextra -Wpedantic
Non deterministic output.
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
0
c++ gcc clang compiler-warnings
1
What deterministic value you would have expected?
– Guillaume Racicot
Mar 8 at 20:48
5
You have undefined behavior. All results are valid
– NathanOliver
Mar 8 at 20:49
Of course this is not the correct way of initializing the variable. I would expect clang to warn me and gcc to warn me not only when optimization is turned on. I would also expect the program to print the same value regardless of the optimization level.
– user3457151
Mar 8 at 20:52
Welcome to UB land. The compiler doesn't have to tell you about it and different implementations can do different things. You can even get different things from each run of the same binary.
– NathanOliver
Mar 8 at 21:05
1
-Weverything is not a good idea
– sp2danny
Mar 8 at 21:17
add a comment |
Consider the following snippet. Class test
has a const member a
and a member function fun
which returns a
. An initialization list is used to to initialize a
in the constructor. However in the initialization list a lambda is used to initialize a
with the returned value of fun. This leads to different behaviors of clang and gcc at compile and runtime, depending on the optimization level. Below the snippet and the different outputs at compile and runtime are listed. Is this expected behavior of gcc and clang?
#include <iostream>
class test
public:
const int a;
test(): a([this]()return fun();())
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Compiletime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
3 warnings generated.
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
g++ -std=c++17 -Wall -Wextra -Wpedantic
No output
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
lambda_in_initializer_list.cpp: In function ‘int main()’:
lambda_in_initializer_list.cpp:18:20: warning: ‘t.test::a’ is used uninitialized in this function [-Wuninitialized]
std::cout << t.a << 'n';
~~^
Runtime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
0
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
4196112
g++ -std=c++17 -Wall -Wextra -Wpedantic
Non deterministic output.
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
0
c++ gcc clang compiler-warnings
Consider the following snippet. Class test
has a const member a
and a member function fun
which returns a
. An initialization list is used to to initialize a
in the constructor. However in the initialization list a lambda is used to initialize a
with the returned value of fun. This leads to different behaviors of clang and gcc at compile and runtime, depending on the optimization level. Below the snippet and the different outputs at compile and runtime are listed. Is this expected behavior of gcc and clang?
#include <iostream>
class test
public:
const int a;
test(): a([this]()return fun();())
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Compiletime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
3 warnings generated.
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
[-Wc++98-compat]
test(): a([this]()return fun();())
^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
[-Wc++98-compat]
auto t = test();
^~~~
g++ -std=c++17 -Wall -Wextra -Wpedantic
No output
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
lambda_in_initializer_list.cpp: In function ‘int main()’:
lambda_in_initializer_list.cpp:18:20: warning: ‘t.test::a’ is used uninitialized in this function [-Wuninitialized]
std::cout << t.a << 'n';
~~^
Runtime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything
0
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1
4196112
g++ -std=c++17 -Wall -Wextra -Wpedantic
Non deterministic output.
g++ -std=c++17 -Wall -Wextra -Wpedantic -O1
0
c++ gcc clang compiler-warnings
c++ gcc clang compiler-warnings
edited Mar 8 at 20:53
Guillaume Racicot
16.6k53872
16.6k53872
asked Mar 8 at 20:44
user3457151user3457151
4314
4314
1
What deterministic value you would have expected?
– Guillaume Racicot
Mar 8 at 20:48
5
You have undefined behavior. All results are valid
– NathanOliver
Mar 8 at 20:49
Of course this is not the correct way of initializing the variable. I would expect clang to warn me and gcc to warn me not only when optimization is turned on. I would also expect the program to print the same value regardless of the optimization level.
– user3457151
Mar 8 at 20:52
Welcome to UB land. The compiler doesn't have to tell you about it and different implementations can do different things. You can even get different things from each run of the same binary.
– NathanOliver
Mar 8 at 21:05
1
-Weverything is not a good idea
– sp2danny
Mar 8 at 21:17
add a comment |
1
What deterministic value you would have expected?
– Guillaume Racicot
Mar 8 at 20:48
5
You have undefined behavior. All results are valid
– NathanOliver
Mar 8 at 20:49
Of course this is not the correct way of initializing the variable. I would expect clang to warn me and gcc to warn me not only when optimization is turned on. I would also expect the program to print the same value regardless of the optimization level.
– user3457151
Mar 8 at 20:52
Welcome to UB land. The compiler doesn't have to tell you about it and different implementations can do different things. You can even get different things from each run of the same binary.
– NathanOliver
Mar 8 at 21:05
1
-Weverything is not a good idea
– sp2danny
Mar 8 at 21:17
1
1
What deterministic value you would have expected?
– Guillaume Racicot
Mar 8 at 20:48
What deterministic value you would have expected?
– Guillaume Racicot
Mar 8 at 20:48
5
5
You have undefined behavior. All results are valid
– NathanOliver
Mar 8 at 20:49
You have undefined behavior. All results are valid
– NathanOliver
Mar 8 at 20:49
Of course this is not the correct way of initializing the variable. I would expect clang to warn me and gcc to warn me not only when optimization is turned on. I would also expect the program to print the same value regardless of the optimization level.
– user3457151
Mar 8 at 20:52
Of course this is not the correct way of initializing the variable. I would expect clang to warn me and gcc to warn me not only when optimization is turned on. I would also expect the program to print the same value regardless of the optimization level.
– user3457151
Mar 8 at 20:52
Welcome to UB land. The compiler doesn't have to tell you about it and different implementations can do different things. You can even get different things from each run of the same binary.
– NathanOliver
Mar 8 at 21:05
Welcome to UB land. The compiler doesn't have to tell you about it and different implementations can do different things. You can even get different things from each run of the same binary.
– NathanOliver
Mar 8 at 21:05
1
1
-Weverything is not a good idea
– sp2danny
Mar 8 at 21:17
-Weverything is not a good idea
– sp2danny
Mar 8 at 21:17
add a comment |
2 Answers
2
active
oldest
votes
I didn't quite understood a question, but it seems like you are actually asking 'why gcc didn't warn you until you turned up optimization'.
This is a known thing. Detecting undefined behavior in complex cases requires quite a lot of efforts on compiler side, and often is only done when you are optimizing code (since compiler is doing a lot of work anyways). Just something to keep in mind when you are dealing with real life-compilers.
add a comment |
You have undefined behaviour. You use the value of a
before initialization. If you want your program to be valid, initialize your variable before using it.
struct test
int a;
test(): a(0) // Effectively return the value of a,
// which is 0 at this point.
// ~~~~~~~v~~~~
a = [this] return fun(); ();
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Your compilers even warned you about your code. Listen to them. The warning were right, your code was invalid.
Clang did not warn me about this. Only gcc at optimazation level one warned me.
– user3457151
Mar 8 at 20:57
@user3457151 yes. Compilers are not required to find all possible warnings one would expect. They're only there to help you if the compiler find something odd.
– Guillaume Racicot
Mar 8 at 20:58
add a comment |
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
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070740%2fusage-before-initialization-of-const-member-is-this-expected-bahviour-of-gcc-an%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I didn't quite understood a question, but it seems like you are actually asking 'why gcc didn't warn you until you turned up optimization'.
This is a known thing. Detecting undefined behavior in complex cases requires quite a lot of efforts on compiler side, and often is only done when you are optimizing code (since compiler is doing a lot of work anyways). Just something to keep in mind when you are dealing with real life-compilers.
add a comment |
I didn't quite understood a question, but it seems like you are actually asking 'why gcc didn't warn you until you turned up optimization'.
This is a known thing. Detecting undefined behavior in complex cases requires quite a lot of efforts on compiler side, and often is only done when you are optimizing code (since compiler is doing a lot of work anyways). Just something to keep in mind when you are dealing with real life-compilers.
add a comment |
I didn't quite understood a question, but it seems like you are actually asking 'why gcc didn't warn you until you turned up optimization'.
This is a known thing. Detecting undefined behavior in complex cases requires quite a lot of efforts on compiler side, and often is only done when you are optimizing code (since compiler is doing a lot of work anyways). Just something to keep in mind when you are dealing with real life-compilers.
I didn't quite understood a question, but it seems like you are actually asking 'why gcc didn't warn you until you turned up optimization'.
This is a known thing. Detecting undefined behavior in complex cases requires quite a lot of efforts on compiler side, and often is only done when you are optimizing code (since compiler is doing a lot of work anyways). Just something to keep in mind when you are dealing with real life-compilers.
answered Mar 8 at 21:18
SergeyASergeyA
45.2k53990
45.2k53990
add a comment |
add a comment |
You have undefined behaviour. You use the value of a
before initialization. If you want your program to be valid, initialize your variable before using it.
struct test
int a;
test(): a(0) // Effectively return the value of a,
// which is 0 at this point.
// ~~~~~~~v~~~~
a = [this] return fun(); ();
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Your compilers even warned you about your code. Listen to them. The warning were right, your code was invalid.
Clang did not warn me about this. Only gcc at optimazation level one warned me.
– user3457151
Mar 8 at 20:57
@user3457151 yes. Compilers are not required to find all possible warnings one would expect. They're only there to help you if the compiler find something odd.
– Guillaume Racicot
Mar 8 at 20:58
add a comment |
You have undefined behaviour. You use the value of a
before initialization. If you want your program to be valid, initialize your variable before using it.
struct test
int a;
test(): a(0) // Effectively return the value of a,
// which is 0 at this point.
// ~~~~~~~v~~~~
a = [this] return fun(); ();
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Your compilers even warned you about your code. Listen to them. The warning were right, your code was invalid.
Clang did not warn me about this. Only gcc at optimazation level one warned me.
– user3457151
Mar 8 at 20:57
@user3457151 yes. Compilers are not required to find all possible warnings one would expect. They're only there to help you if the compiler find something odd.
– Guillaume Racicot
Mar 8 at 20:58
add a comment |
You have undefined behaviour. You use the value of a
before initialization. If you want your program to be valid, initialize your variable before using it.
struct test
int a;
test(): a(0) // Effectively return the value of a,
// which is 0 at this point.
// ~~~~~~~v~~~~
a = [this] return fun(); ();
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Your compilers even warned you about your code. Listen to them. The warning were right, your code was invalid.
You have undefined behaviour. You use the value of a
before initialization. If you want your program to be valid, initialize your variable before using it.
struct test
int a;
test(): a(0) // Effectively return the value of a,
// which is 0 at this point.
// ~~~~~~~v~~~~
a = [this] return fun(); ();
int fun()
return a;
;
int main()
auto t = test();
std::cout << t.a << 'n';
return 0;
Your compilers even warned you about your code. Listen to them. The warning were right, your code was invalid.
edited Mar 8 at 20:57
answered Mar 8 at 20:53
Guillaume RacicotGuillaume Racicot
16.6k53872
16.6k53872
Clang did not warn me about this. Only gcc at optimazation level one warned me.
– user3457151
Mar 8 at 20:57
@user3457151 yes. Compilers are not required to find all possible warnings one would expect. They're only there to help you if the compiler find something odd.
– Guillaume Racicot
Mar 8 at 20:58
add a comment |
Clang did not warn me about this. Only gcc at optimazation level one warned me.
– user3457151
Mar 8 at 20:57
@user3457151 yes. Compilers are not required to find all possible warnings one would expect. They're only there to help you if the compiler find something odd.
– Guillaume Racicot
Mar 8 at 20:58
Clang did not warn me about this. Only gcc at optimazation level one warned me.
– user3457151
Mar 8 at 20:57
Clang did not warn me about this. Only gcc at optimazation level one warned me.
– user3457151
Mar 8 at 20:57
@user3457151 yes. Compilers are not required to find all possible warnings one would expect. They're only there to help you if the compiler find something odd.
– Guillaume Racicot
Mar 8 at 20:58
@user3457151 yes. Compilers are not required to find all possible warnings one would expect. They're only there to help you if the compiler find something odd.
– Guillaume Racicot
Mar 8 at 20:58
add a comment |
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.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070740%2fusage-before-initialization-of-const-member-is-this-expected-bahviour-of-gcc-an%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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
1
What deterministic value you would have expected?
– Guillaume Racicot
Mar 8 at 20:48
5
You have undefined behavior. All results are valid
– NathanOliver
Mar 8 at 20:49
Of course this is not the correct way of initializing the variable. I would expect clang to warn me and gcc to warn me not only when optimization is turned on. I would also expect the program to print the same value regardless of the optimization level.
– user3457151
Mar 8 at 20:52
Welcome to UB land. The compiler doesn't have to tell you about it and different implementations can do different things. You can even get different things from each run of the same binary.
– NathanOliver
Mar 8 at 21:05
1
-Weverything is not a good idea
– sp2danny
Mar 8 at 21:17