Is it possible to prevent the optimization of static constant constants 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;
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 characteristic of this code is that classes are templates. Perhaps there is something in the C ++ standard that makes the compiler optimize fields, even with -O0? When I do not use templates, the values are not optimized:
struct foo { static const unsigned x1 = 1; };
In this case, I see x1 in the debugger
source share