Consider the following simple code:
template<typename T>
struct Base {
static constexpr int v = 0;
};
struct Derived : Base<int> {};
int main() {
Derived d;
}
I compile it with GCC (8.3.0, 9.1.0, g++ -g -O0 test.cpp
), then run GDB to examine the value of d
:
(gdb) p d
$1 = {<Base<int>> = {static v = <optimized out>}, <No data fields>}
d.v
is gone... I tried to use -ggdb
, -ggdb3
, -fvar-tracking
with no success.
If I compile with clang (clang++ -g -O0 test.cpp
), I see the expected result:
(gdb) p d
$1 = {<Base<int>> = {static v = 0}, <No data fields>}
This output is also seen with GCC if Base
is not a template class:
(gdb) p d
$1 = {<Base> = {static v = 0}, <No data fields>}
Where did d.v
go? Why was it optimized out? Is it possible to prevent this optimization without modifying the source code?