0

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.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
bombax
  • 1,189
  • 8
  • 26
  • A compiler does not assume anything. A compiler compiles the code according to what the code says. If an object is declared as `const`, the compiler considers it to be a `const` object. If an object is not declared as `const`, the compiler will not consider it to be a `const` object. The End. – Sam Varshavchik Sep 11 '16 at 23:40
  • 1
    Why does it matter that the compiler assumes `globalBar` is `const` in `foo()`? – Cornstalks Sep 11 '16 at 23:41
  • For optimization reasons. I have very few cycles to play with in this code. – bombax Sep 11 '16 at 23:41
  • 2
    Agreed with @SamVarshavchik. What you could do is declare it const and assign to it in the declaration. However, beware of doing this with globals since you can easily die in the fire of the [static initialization order fiasco](http://stackoverflow.com/questions/1005685/c-static-initialization-order). You should consider a singleton pattern, which will allow you to access your global object through a function that returns a const reference. – Nicholas Smith Sep 11 '16 at 23:44
  • 1
    Can you make the global variable const and then use a const cast in the setup function to assign to it? Then it is const for the entire rest of the program – Jerry Jeremiah Sep 11 '16 at 23:44
  • Thanks for your helpful comments! This is the kind of info I need to understand the assumptions that are safe for the compiler to make about const objects. – bombax Sep 11 '16 at 23:45
  • 3
    @JerryJeremiah -- modifying an object that is defined as const produces undefined behavior. – Pete Becker Sep 11 '16 at 23:45
  • @PeteBecker Good point - I should think before talking. – Jerry Jeremiah Sep 11 '16 at 23:47
  • Try to find a way to avoid the global. Singleton/Monostate pattern or just pass it around. With globals you surrender such fine grained control. – Paul Rooney Sep 12 '16 at 00:18
  • Unfortunately, this isn't possible as you want it in C++, at least not to my knowledge. While it would be a godsend in certain situations (such as when working with legacy code that uses tons of globals, or in extremely resource-limited systems), you'll have to make do with something else. – Justin Time - Reinstate Monica Sep 12 '16 at 00:28
  • Thanks @JustinTime but how about my proposed answer below? Is that possible and/or safe? – bombax Sep 12 '16 at 00:49

1 Answers1

0

This is a theoretical example. I haven't tested it but would love opinions from C++ experts. Would this work?

static Bar globalBar;

static void foo() {
    static const Bar myGlobalBar = globalBar;
    int x = myGlobalBar .x; // the compiler should assume globalBar is const here!
    ...
}
bombax
  • 1,189
  • 8
  • 26