News for hackers. Stuff that matters.
Re:std::launder: the most obscure new feature of C++17 by amluto on Thursday April 04, 2024 @05:35PM (#39936044)
C++’s const was never especially safe — contemplate what mutable does. And that one can cast pointers-to-nonconst to pointer-to-const.
Re:std::launder: the most obscure new feature of C++17 by kimixa on Thursday April 04, 2024 @05:38PM (#39936079)
It's always been a "programmer aid annotation" rather than anything that really affects code generation.

The only possible difference (from the resulting binary POV) I can think of is "static const" variables might be allocated in a read-only executable section?

Re:std::launder: the most obscure new feature of C++17 by PhilipRoman on Thursday April 04, 2024 @06:04PM (#39936360)
It doesn't even have to be static. Const is powerful when used on non-pointer types. You can write something like this and the compiler will (on appropriate optimization levels) hard-code return value to 7777 regardless of what "mutate" does:

    extern void mutate(int *p);
    int func() {
        const int foo = 7777;
        mutate(&foo);
        return foo;
    }
Re:std::launder: the most obscure new feature of C++17 by throwway120385 on Thursday April 04, 2024 @05:43PM (#39936133)
Probably not, given the possibility that they can be const_casted back into mutable values. Locating static const values in read-only pages would result in crashes from valid statements in the language. In other words if you put a static const in a read only page, it would probably be considered a bug in your linker.
Re:std::launder: the most obscure new feature of C++17 by actionfromafar on Thursday April 04, 2024 @05:50PM (#39936203)
That doesn't rhyme with my understanding of C++ (which is limited, of course). This sounds like undefined.
Re:std::launder: the most obscure new feature of C++17 by secondcoming on Thursday April 04, 2024 @05:56PM (#39936275)
Aren't they put into the .rodata section of the binary?

Crashing is exactly what happens, thankfully

https://godbolt.org/z/vKj9dcPMc

Re:std::launder: the most obscure new feature of C++17 by gary_0 on Thursday April 04, 2024 @06:26PM (#39936556)
Generally yes, if you have a bunch of const data in your C++ code, it will go in .rodata. Depending on the compiler/flags/optimizations, of course. https://stackoverflow.com/a/44938843
Re:std::launder: the most obscure new feature of C++17 by gary_0 on Thursday April 04, 2024 @06:16PM (#39936483)
const_cast is only for removing const from references or pointers that actually refer to a non-const object. You can't declare a const variable and then modify it with const_cast. See "Notes" at: https://en.cppreference.com/w/cpp/language/const_cast

The compiler can indeed put const objects in the executable's .rodata section, which means trying to modify it at runtime will probably cause a segfault (but it's all Undefined Behavior so who knows).

Re:std::launder: the most obscure new feature of C++17 by bingo3131 on Thursday April 04, 2024 @06:44PM (#39936719)
It is undefined behaviour to modify objects declared as const (except if those objects have mutable members, in which case the mutable members can be modified).

const int a = 1;

If you try to const_cast away the const of x to change its value, you have undefined behaviour. As for pointers, if you make a const pointer to a non-const object then all it means is that you cannot modify the object via that pointer, not that the object will never be modified.

int a = 1; int b = &a; const int c = &a;

a = 2;

Both b and c are now 2 and this is absolutely fine, and the compiler won't try to optimise out any reads of *c and assume its value is still 1 as const/non-const pointer aliasing is allowed (C has the restrict keyword, it's not in C++).