Is it possible to prevent static const members optimization using compiler command-line options?
Here is an example:
template <unsigned v1>
struct TRAITS {
static const unsigned val1 = v1;
};
template < class TRAITS >
struct foo {
static const unsigned x1 = TRAITS::val1;
};
int main () {
foo<TRAITS<1>> f1;
// SET BREAKPOINT HERE
return 0;
}
Compile:
g++ -g -O0 optimize_out.cpp
GDB:
gdb a.out
(gdb) break optimize_out.cpp:13
(gdb) r
(gdb) p f1
$1 = {static x1 = <optimized out>}
What is specific about this code is that classes are templates. Probably there is something in C++ standard that forces compiler to optimize fields away, even with -O0 ? When I don't use templates, values are not optimized away:
struct foo {
static const unsigned x1 = 1;
};
In this case I can see x1 in debugger