Preprocessor and template arguments or conditional compilation of piece of code

前端 未结 4 922
抹茶落季
抹茶落季 2021-01-18 19:05

How I can compile template function with pre-processor condition? Like that (but it is not working):

template 
void f()
{
    #if (var == tru         


        
4条回答
  •  盖世英雄少女心
    2021-01-18 19:21

    With C++17's introduction of if constexpr you can discard branches inside a template, much like conditional compilation allows.

    template 
    void f()
    {
        if constexpr (var == true) {
        // ...
        }
    }
    

    The code inside the branch has to be syntactically correct, but doesn't have to be well-formed when var is false, because it will be discarded entirely.

提交回复
热议问题