I have the following setup:
Foo.cpp
class Bar {
public:
inline Bar() : x(0), y(0), z(0) {}
inline Bar(int X, int Y, int Z) : x(X), y(Y), z(Z) {}
const int x, y, z;
};
static Bar globalBar;
static void foo() {
int x = globalBar.x; // the compiler should assume globalBar is const here!
...
}
void almightySetup() {
globalBar = Bar(meaningOfLife(), complexCalc(), magic());
startThread(foo); // foo() will NEVER be called before this point!
// globalBar will NEVER be changed after this point!
}
As you can see, it is safe for the compiler to assume that globalBar
is const
at the indicated point, because globalBar
, after setup, will never be changed after that point. Furthermore, foo()
will not be called before it is setup.
But how can I accomplish this? I've tried using const_cast<>
but keep getting type error messages. I must be doing something wrong. Is it even possible?
I should add that I am not at liberty to change the function signature of foo
.