I would like to break the compilation if the object is declared const.
The following doesn't work :
#include <type_traits>
struct A {
A() : v(0)
{
static_assert( ! std::is_const<decltype(*this)>::value, "declared as const" );
}
int& AccessValue() const
{
return const_cast< int& >( v );
}
int v;
};
int main()
{
A a1; // ok, this compiles
const A a2; // no, this break the compilation
a1.AccessValue() = 5; // ok
a2.AccessValue() = 6; // OPS
}
So, is there a way to break the compilation if an object of this type is declared const?