optimization - Why C++ compiler isn't optimizing unused reference variables? -
consider following program:
#include <iostream> struct test { int& ref1; int& ref2; int& ref3; }; int main() { std::cout<<sizeof(test)<<'\n'; }
i know c++ compiler can optimize reference variables entirely won't take space in memory @ all.
i tested above demo program see output. when compile & run on g++ 4.8.1 gives me output 12. looks compiler isn't optimizing reference variables. expecting size of test struct 1. i've used -os
command line option still gives me output 12. have tried program on msvs 2010 compiled /ox
command line option looks microsoft compiler isn't performing optimization @ all.
the 3 reference variables unused & aren't associated other variable. why compilers aren't optimizing them?
the size of struct stays same, there nothing optimize. if create array of test
should allocate right size each test
. compiler cannot know used or not. that's why there no such optimization.
unused variables example new int& int
inside main function. if unused, optimizer optimize away.
Comments
Post a Comment