预处理器和模板参数或代码片段的条件编译

时间:2021-08-17 22:30:52

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

如何使用预处理器条件编译模板功能?像那样(但它不起作用):

template <bool var>
void f()
{
    #if (var == true)
    // ...
    #endif
}

3 个解决方案

#1


9  

You can't. The preprocessor, as this names indicates, processes the source file before the compiler. It has therefore no knowledge of the values of your template arguments.

你不能。正如此名称所示,预处理器在编译器之前处理源文件。因此,它不知道模板参数的值。

#2


7  

You can't do that with the preprocessor. All you can do is delegate the code to a separate template, something like this:

你不能用预处理器做到这一点。您所能做的就是将代码委托给一个单独的模板,如下所示:

template <bool var>
void only_if_true()
{}

template <>
void only_if_true<true>()
{
  your_special_code_here();
}


template <bool var>
void f()
{
  some_code_always_used();
  only_if_true<var>();
  some_code_always_used();
}

Of course, if you need information shared between f() and only_if_true() (which is likely), you have to pass it as parameters. Or make only_if_true a class and store the shared data in it.

当然,如果需要f()和only_if_true()之间共享的信息(可能),则必须将其作为参数传递。或者将only_if_true设为一个类并将共享数据存储在其中。

#3


3  

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

如果需要使用模板参数生成不同的代码路径,则只需使用if或其他C ++语句:

template <bool var>
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.

一个小缺点是某些编译器(例如Msvc)将为始终不变的条件生成警告。

#1


9  

You can't. The preprocessor, as this names indicates, processes the source file before the compiler. It has therefore no knowledge of the values of your template arguments.

你不能。正如此名称所示,预处理器在编译器之前处理源文件。因此,它不知道模板参数的值。

#2


7  

You can't do that with the preprocessor. All you can do is delegate the code to a separate template, something like this:

你不能用预处理器做到这一点。您所能做的就是将代码委托给一个单独的模板,如下所示:

template <bool var>
void only_if_true()
{}

template <>
void only_if_true<true>()
{
  your_special_code_here();
}


template <bool var>
void f()
{
  some_code_always_used();
  only_if_true<var>();
  some_code_always_used();
}

Of course, if you need information shared between f() and only_if_true() (which is likely), you have to pass it as parameters. Or make only_if_true a class and store the shared data in it.

当然,如果需要f()和only_if_true()之间共享的信息(可能),则必须将其作为参数传递。或者将only_if_true设为一个类并将共享数据存储在其中。

#3


3  

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

如果需要使用模板参数生成不同的代码路径,则只需使用if或其他C ++语句:

template <bool var>
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.

一个小缺点是某些编译器(例如Msvc)将为始终不变的条件生成警告。