I know that constant variables outside classes can be optimized directly into function calls by the compiler, but is it legal for the compiler to do the same for constant class variables?
If there is a class declared like this:
class A {
public:
const int constVar;
//other, modifiable variables
A(int val): constVar(val) {
//code to initialize modifiable variables
}
};
and I create an instance of A and call a function like this:
A obj(-2);
int absoluteVal = std::abs(A.constVar);
is the compiler allowed to do this instead and make the class be sizeof(int) smaller?:
A obj();
int absoluteVal = std::abs(-2);