[LLVMdev] weak_odr constant versus weak_odr global

Eli Friedman
Mon, 07 Nov 2011 16:31:16 -0000

2011/11/7 Rafael Espíndola <rafael.espindola at
gmail.com>:
> I tried a small variation:
>
> struct S {
>  static const int x;
> };
> template<typename T> struct U {
>  static const int k;
> };
> template<typename T> const int U<T>::k = T::x;
>
> #ifdef TU1
> extern int f();
> const int S::x = 42;
> int main() {
> return f() + reinterpret_cast<long>(&U::k);
> }
> #endif
> #ifdef TU2
> int f() { return U::k; }
> #endif
>
> and it crashes with gcc too :-( The original testcase works because
> gcc folds the value into main even at -O0.
>
> If we are going to support it for real, I think Richard patch is going
> on the right direction, but we should completely drop the idea of a
> "weak_odr constant". If we cannot use it for instructing the
linker to
> put it an o ro section, the "constant" gives us nothing that the
"odr"
> doesn't.

In cases where the C++ standard requires static initialization,
introducing a write violates the guarantees of the C++ standard for
static initialization.  Therefore, I'm not sure the whole "make the
constant writable" approach is actually viable.

-Eli