C++ use templates to avoid compiler from checking a boolean

后端 未结 5 672
忘了有多久
忘了有多久 2021-02-12 13:51

Let\'s say I have a function:

template 
inline void doSomething() {
    if(stuff) {
        cout << \"Hello\" << endl;
    }
    el         


        
5条回答
  •  离开以前
    2021-02-12 14:25

    Indeed, it really createa two functions, but

    premature optimization is the root of all evil

    especially if your changing your code structure because of a simple if statement. I doubt that this will affect performance. Also the boolean must be static, that means you cant take a runtime evaluated var and pass it to the function. How should the linker know which function to call? In this case youll have to manually evaluate it and call the appropiate function on your own.

提交回复
热议问题