What kind of dead code can GCC eliminate from the final output?

后端 未结 3 493
深忆病人
深忆病人 2020-12-14 09:51

I have always been told that compiler is sufficient smart to eliminate dead code. Much of the code that I am writing has a lot of information known at compile time but the c

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 10:37

    When I used template parameter constant in such if expression then dce (Dead Code Elimination) compiler (GCC 4.8.1 on Linux) flags did not helped and O2, O3 optimization also did not helped. I had to use template specialization wrapper:

    template
    f();
    
    template<>
    f(){
      //some code on true condition
    }
    
    template<>
    f(){
      //some code on false condition
    }
    

    Also macros can be used to avoid compilation of the unused code branch, but it depends on the compiler (whether it processed macros as they occur in the code or on precompiling stage):

    template
    f(){
     #if b
      //some code
     #elif
      //some code
     #endif  // b
    }
    

提交回复
热议问题