Preprocessor and template arguments or conditional compilation of piece of code

前端 未结 4 954
抹茶落季
抹茶落季 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

    If you need to generate different code paths with template parameter, you can just simply use if or other C++ statement:

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

    Compiler can optimize it and generate code that doesn't contain such branches.

    A little drawback is that some compiler (e.g. Msvc) will generate warnings for conditions which is always constant.

提交回复
热议问题