Playing around with constexpr
and realised I was able to cast away the constness in much the same way as a regular const object:
#include <iostream>
int main()
{
// your code goes here
constexpr int foo = 10;
int* bad = const_cast<int*>(&foo);
*bad = 5;
std::cout<<*bad<<'\n';
return 0;
}
Will print 5, and I was surprised that this even compiled? However when making the variable static I was even more confused:
#include <iostream>
int main()
{
// your code goes here
static constexpr int foo = 10;
int* bad = const_cast<int*>(&foo);
*bad = 5;
std::cout<<*bad<<'\n';
return 0;
}
This prints 10, meaning that the assignment operator silently failed, again no warnings at compile time.
Is there a section in the standard that explains the behaviour of const_cast
on constexpr
objects?