As a follow up to my previous question (Writing to class member through const &), is it also well defined and correct to cast away a class member method's const-ness like this?
class A
{
public:
A()
: a(5)
{
}
int run() const
{
std::cout << "a: " << a << std::endl;
int& x = (int&)a;
x = 17;
std::cout << "a: " << a << std::endl;
return 0;
}
private:
int a;
};
int main()
{
A program;
return program.run();
}
Output (tested with c++14 on cpp.sh with -O0, -Wall, -Wextra and -Wpedantic):
a: 5
a: 17
If not, what part of the standard would i refer to for an explanation?