Why is const int fine for char brace init?Why can't I convert 'char**' to a 'const char* const*' in C?Why use...
Are there any space probes or landers which regained communication after being lost?
Are Democrats more likely to believe Astrology is a science?
What happens when a caster loses concentration on a banished creature?
How can I protect myself in case of a human attack like the murders of the hikers Jespersen and Ueland in Morocco?
Is there a basic list of ways in which a low-level Rogue can get advantage for sneak attack?
Has any object launched from Earth gone into the Sun?
Why would "an mule" be used instead of "a mule"?
Why is differential privacy defined over the exponential function?
Was Robin Hood's point of view ethically sound?
Is there a star over my head?
Are scroll bars dead in 2019?
Does the word “uzi” need to be capitalized?
Awesomism and its awesome gods
How can "life" insurance prevent the cheapening of death?
Procedure for traffic not in sight
Are there any instances of members of different Hogwarts houses coupling up and marrying each other?
What are the advantages and disadvantages of Preprints.org compared with arXiv?
A medieval fantasy adventurer lights a torch in a 100% pure oxygen room. What happens?
Do any aircraft carry boats?
Are there take-over requests from autopilots?
Why does F + F' = 1?
How would a village use its river that it shares with another village downstream?
Number of aircraft to operate in an airline company
How can I fix a framing mistake so I can drywall?
Why is const int fine for char brace init?
Why can't I convert 'char**' to a 'const char* const*' in C?Why use static_cast<int>(x) instead of (int)x?How to convert a std::string to const char* or char*?What is the difference between char * const and const char *?What is the difference between const int*, const int * const, and int const *?assigning char to int reference and const int reference in C++Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInitialize vector <char> with int valuesCannot initialize a vector of const char*/string array with an initializer-list on declaration
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I thought brace initialization doesn't allow narrowing. But why is int const allowed for char brace initialization?
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
See it on Godbolt.
c++ c++11 const narrowing
add a comment |
I thought brace initialization doesn't allow narrowing. But why is int const allowed for char brace initialization?
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
See it on Godbolt.
c++ c++11 const narrowing
add a comment |
I thought brace initialization doesn't allow narrowing. But why is int const allowed for char brace initialization?
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
See it on Godbolt.
c++ c++11 const narrowing
I thought brace initialization doesn't allow narrowing. But why is int const allowed for char brace initialization?
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
See it on Godbolt.
c++ c++11 const narrowing
c++ c++11 const narrowing
edited 11 hours ago
Boann
39k13 gold badges93 silver badges123 bronze badges
39k13 gold badges93 silver badges123 bronze badges
asked 12 hours ago
BK C.BK C.
1831 gold badge2 silver badges9 bronze badges
1831 gold badge2 silver badges9 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
const int value2 = 12;
value2 is a compile-time constant. The compiler can easily (and has to) prove that the value is 12 which happens to be within the range of values representable by char.
int value1 = 12;
value1 is not a compile-time constant. The value of the variable could change at runtime.
The exact wording of the standard rule (quoting latest draft, emphasis added):
[dcl.init.list]/7
A narrowing conversion is an implicit conversion
- from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.
add a comment |
#include <iostream>
#include <vector>
int main() {
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
}
Warning thrown by your program.
test.cpp: In function ‘int main()’:
test.cpp:7:19: warning: narrowing conversion of ‘value1’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
char c1{value1}; // error! no narrowing
^
Its happens because const int 12 is optimized by compiler to literal 12. While int value1 is not compile time constant(although known at compile time in this case).
Note: For c++11 or greater, contrexpr is recommended.
3
This reasoning is wrong. Compiler optimizations have not happened at the point where the compiler decides whether the code is valid or not. And you get the same error even without any optimizations. Further,value1is definitely not opaque to compiler optimizations. It's just thatvalue2is a constant expression whilevalue1is not.
– Max Langhof
11 hours ago
Corrected, @MaxLanghof
– v78
11 hours ago
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/4.0/"u003ecc by-sa 4.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%2f57855180%2fwhy-is-const-int-fine-for-char-brace-init%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
const int value2 = 12;
value2 is a compile-time constant. The compiler can easily (and has to) prove that the value is 12 which happens to be within the range of values representable by char.
int value1 = 12;
value1 is not a compile-time constant. The value of the variable could change at runtime.
The exact wording of the standard rule (quoting latest draft, emphasis added):
[dcl.init.list]/7
A narrowing conversion is an implicit conversion
- from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.
add a comment |
const int value2 = 12;
value2 is a compile-time constant. The compiler can easily (and has to) prove that the value is 12 which happens to be within the range of values representable by char.
int value1 = 12;
value1 is not a compile-time constant. The value of the variable could change at runtime.
The exact wording of the standard rule (quoting latest draft, emphasis added):
[dcl.init.list]/7
A narrowing conversion is an implicit conversion
- from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.
add a comment |
const int value2 = 12;
value2 is a compile-time constant. The compiler can easily (and has to) prove that the value is 12 which happens to be within the range of values representable by char.
int value1 = 12;
value1 is not a compile-time constant. The value of the variable could change at runtime.
The exact wording of the standard rule (quoting latest draft, emphasis added):
[dcl.init.list]/7
A narrowing conversion is an implicit conversion
- from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.
const int value2 = 12;
value2 is a compile-time constant. The compiler can easily (and has to) prove that the value is 12 which happens to be within the range of values representable by char.
int value1 = 12;
value1 is not a compile-time constant. The value of the variable could change at runtime.
The exact wording of the standard rule (quoting latest draft, emphasis added):
[dcl.init.list]/7
A narrowing conversion is an implicit conversion
- from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.
edited 11 hours ago
Max Langhof
15.9k3 gold badges26 silver badges48 bronze badges
15.9k3 gold badges26 silver badges48 bronze badges
answered 11 hours ago
eerorikaeerorika
105k6 gold badges86 silver badges162 bronze badges
105k6 gold badges86 silver badges162 bronze badges
add a comment |
add a comment |
#include <iostream>
#include <vector>
int main() {
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
}
Warning thrown by your program.
test.cpp: In function ‘int main()’:
test.cpp:7:19: warning: narrowing conversion of ‘value1’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
char c1{value1}; // error! no narrowing
^
Its happens because const int 12 is optimized by compiler to literal 12. While int value1 is not compile time constant(although known at compile time in this case).
Note: For c++11 or greater, contrexpr is recommended.
3
This reasoning is wrong. Compiler optimizations have not happened at the point where the compiler decides whether the code is valid or not. And you get the same error even without any optimizations. Further,value1is definitely not opaque to compiler optimizations. It's just thatvalue2is a constant expression whilevalue1is not.
– Max Langhof
11 hours ago
Corrected, @MaxLanghof
– v78
11 hours ago
add a comment |
#include <iostream>
#include <vector>
int main() {
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
}
Warning thrown by your program.
test.cpp: In function ‘int main()’:
test.cpp:7:19: warning: narrowing conversion of ‘value1’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
char c1{value1}; // error! no narrowing
^
Its happens because const int 12 is optimized by compiler to literal 12. While int value1 is not compile time constant(although known at compile time in this case).
Note: For c++11 or greater, contrexpr is recommended.
3
This reasoning is wrong. Compiler optimizations have not happened at the point where the compiler decides whether the code is valid or not. And you get the same error even without any optimizations. Further,value1is definitely not opaque to compiler optimizations. It's just thatvalue2is a constant expression whilevalue1is not.
– Max Langhof
11 hours ago
Corrected, @MaxLanghof
– v78
11 hours ago
add a comment |
#include <iostream>
#include <vector>
int main() {
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
}
Warning thrown by your program.
test.cpp: In function ‘int main()’:
test.cpp:7:19: warning: narrowing conversion of ‘value1’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
char c1{value1}; // error! no narrowing
^
Its happens because const int 12 is optimized by compiler to literal 12. While int value1 is not compile time constant(although known at compile time in this case).
Note: For c++11 or greater, contrexpr is recommended.
#include <iostream>
#include <vector>
int main() {
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
}
Warning thrown by your program.
test.cpp: In function ‘int main()’:
test.cpp:7:19: warning: narrowing conversion of ‘value1’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
char c1{value1}; // error! no narrowing
^
Its happens because const int 12 is optimized by compiler to literal 12. While int value1 is not compile time constant(although known at compile time in this case).
Note: For c++11 or greater, contrexpr is recommended.
edited 11 hours ago
answered 11 hours ago
v78v78
1,9669 silver badges21 bronze badges
1,9669 silver badges21 bronze badges
3
This reasoning is wrong. Compiler optimizations have not happened at the point where the compiler decides whether the code is valid or not. And you get the same error even without any optimizations. Further,value1is definitely not opaque to compiler optimizations. It's just thatvalue2is a constant expression whilevalue1is not.
– Max Langhof
11 hours ago
Corrected, @MaxLanghof
– v78
11 hours ago
add a comment |
3
This reasoning is wrong. Compiler optimizations have not happened at the point where the compiler decides whether the code is valid or not. And you get the same error even without any optimizations. Further,value1is definitely not opaque to compiler optimizations. It's just thatvalue2is a constant expression whilevalue1is not.
– Max Langhof
11 hours ago
Corrected, @MaxLanghof
– v78
11 hours ago
3
3
This reasoning is wrong. Compiler optimizations have not happened at the point where the compiler decides whether the code is valid or not. And you get the same error even without any optimizations. Further,
value1 is definitely not opaque to compiler optimizations. It's just that value2 is a constant expression while value1 is not.– Max Langhof
11 hours ago
This reasoning is wrong. Compiler optimizations have not happened at the point where the compiler decides whether the code is valid or not. And you get the same error even without any optimizations. Further,
value1 is definitely not opaque to compiler optimizations. It's just that value2 is a constant expression while value1 is not.– Max Langhof
11 hours ago
Corrected, @MaxLanghof
– v78
11 hours ago
Corrected, @MaxLanghof
– v78
11 hours ago
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%2f57855180%2fwhy-is-const-int-fine-for-char-brace-init%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