const cast and std launder
Modifying a const constructed object after a const_cast is UB (I believe due to constant propagation). Is it still UB even when combined with std::launder
(which AFAIK prevents some optimizations such as const propagation)?
#include <new>
#include <iostream>
struct C
{
int i;
};
int main(const int argc, const char * const * const argv)
{
const C c{1};
auto x = std::launder(const_cast<C*>(&c));
++x->i;
std::cout << x->i << std::endl;
std::cout << c.i << std::endl;
return 0;
}
c++ c++17
add a comment |
Modifying a const constructed object after a const_cast is UB (I believe due to constant propagation). Is it still UB even when combined with std::launder
(which AFAIK prevents some optimizations such as const propagation)?
#include <new>
#include <iostream>
struct C
{
int i;
};
int main(const int argc, const char * const * const argv)
{
const C c{1};
auto x = std::launder(const_cast<C*>(&c));
++x->i;
std::cout << x->i << std::endl;
std::cout << c.i << std::endl;
return 0;
}
c++ c++17
2
Modifying const objects is even given as an example of undefined behavior in standard [intro.execution]/4: "Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object)." Why to try to find loopholes in it?
– Öö Tiib
Nov 15 '18 at 10:46
The purpose ofstd::launder
is not to "prevent some optimizations". That's the wrong way to look at it. Its purpose is to communicate a specific thing to a compiler which in turn would allow it to do "the right thing".
– Dan M.
Nov 15 '18 at 11:00
I guess I have put it wrong, perhaps 'to prevent the compiler from making the wrong assumptions' would have been a better way to express it.
– mkmostafa
Nov 15 '18 at 11:06
add a comment |
Modifying a const constructed object after a const_cast is UB (I believe due to constant propagation). Is it still UB even when combined with std::launder
(which AFAIK prevents some optimizations such as const propagation)?
#include <new>
#include <iostream>
struct C
{
int i;
};
int main(const int argc, const char * const * const argv)
{
const C c{1};
auto x = std::launder(const_cast<C*>(&c));
++x->i;
std::cout << x->i << std::endl;
std::cout << c.i << std::endl;
return 0;
}
c++ c++17
Modifying a const constructed object after a const_cast is UB (I believe due to constant propagation). Is it still UB even when combined with std::launder
(which AFAIK prevents some optimizations such as const propagation)?
#include <new>
#include <iostream>
struct C
{
int i;
};
int main(const int argc, const char * const * const argv)
{
const C c{1};
auto x = std::launder(const_cast<C*>(&c));
++x->i;
std::cout << x->i << std::endl;
std::cout << c.i << std::endl;
return 0;
}
c++ c++17
c++ c++17
edited Nov 15 '18 at 10:42
Jarod42
118k12103188
118k12103188
asked Nov 15 '18 at 10:26
mkmostafamkmostafa
1,2431028
1,2431028
2
Modifying const objects is even given as an example of undefined behavior in standard [intro.execution]/4: "Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object)." Why to try to find loopholes in it?
– Öö Tiib
Nov 15 '18 at 10:46
The purpose ofstd::launder
is not to "prevent some optimizations". That's the wrong way to look at it. Its purpose is to communicate a specific thing to a compiler which in turn would allow it to do "the right thing".
– Dan M.
Nov 15 '18 at 11:00
I guess I have put it wrong, perhaps 'to prevent the compiler from making the wrong assumptions' would have been a better way to express it.
– mkmostafa
Nov 15 '18 at 11:06
add a comment |
2
Modifying const objects is even given as an example of undefined behavior in standard [intro.execution]/4: "Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object)." Why to try to find loopholes in it?
– Öö Tiib
Nov 15 '18 at 10:46
The purpose ofstd::launder
is not to "prevent some optimizations". That's the wrong way to look at it. Its purpose is to communicate a specific thing to a compiler which in turn would allow it to do "the right thing".
– Dan M.
Nov 15 '18 at 11:00
I guess I have put it wrong, perhaps 'to prevent the compiler from making the wrong assumptions' would have been a better way to express it.
– mkmostafa
Nov 15 '18 at 11:06
2
2
Modifying const objects is even given as an example of undefined behavior in standard [intro.execution]/4: "Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object)." Why to try to find loopholes in it?
– Öö Tiib
Nov 15 '18 at 10:46
Modifying const objects is even given as an example of undefined behavior in standard [intro.execution]/4: "Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object)." Why to try to find loopholes in it?
– Öö Tiib
Nov 15 '18 at 10:46
The purpose of
std::launder
is not to "prevent some optimizations". That's the wrong way to look at it. Its purpose is to communicate a specific thing to a compiler which in turn would allow it to do "the right thing".– Dan M.
Nov 15 '18 at 11:00
The purpose of
std::launder
is not to "prevent some optimizations". That's the wrong way to look at it. Its purpose is to communicate a specific thing to a compiler which in turn would allow it to do "the right thing".– Dan M.
Nov 15 '18 at 11:00
I guess I have put it wrong, perhaps 'to prevent the compiler from making the wrong assumptions' would have been a better way to express it.
– mkmostafa
Nov 15 '18 at 11:06
I guess I have put it wrong, perhaps 'to prevent the compiler from making the wrong assumptions' would have been a better way to express it.
– mkmostafa
Nov 15 '18 at 11:06
add a comment |
2 Answers
2
active
oldest
votes
const object - an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.
add a comment |
Yes. Attempting to modify a const object is UB, period.
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%2f53317303%2fconst-cast-and-std-launder%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 object - an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.
add a comment |
const object - an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.
add a comment |
const object - an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.
const object - an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.
answered Nov 15 '18 at 10:37
P.WP.W
15.6k31453
15.6k31453
add a comment |
add a comment |
Yes. Attempting to modify a const object is UB, period.
add a comment |
Yes. Attempting to modify a const object is UB, period.
add a comment |
Yes. Attempting to modify a const object is UB, period.
Yes. Attempting to modify a const object is UB, period.
answered Nov 15 '18 at 10:30
T.C.T.C.
107k14220328
107k14220328
add a comment |
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%2f53317303%2fconst-cast-and-std-launder%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
2
Modifying const objects is even given as an example of undefined behavior in standard [intro.execution]/4: "Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object)." Why to try to find loopholes in it?
– Öö Tiib
Nov 15 '18 at 10:46
The purpose of
std::launder
is not to "prevent some optimizations". That's the wrong way to look at it. Its purpose is to communicate a specific thing to a compiler which in turn would allow it to do "the right thing".– Dan M.
Nov 15 '18 at 11:00
I guess I have put it wrong, perhaps 'to prevent the compiler from making the wrong assumptions' would have been a better way to express it.
– mkmostafa
Nov 15 '18 at 11:06