Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使预处理器获取模板参数?_C++_C Preprocessor - Fatal编程技术网

C++ 如何使预处理器获取模板参数?

C++ 如何使预处理器获取模板参数?,c++,c-preprocessor,C++,C Preprocessor,我正在尝试hiper优化一段代码,我发现如果我将一个操作分为两部分,以防程序员希望以true或false的形式运行该方法,那么程序运行速度将提高10%,因此我尝试这样做: template<bool way> void action(){ #if way cout << "Do true actions" << endl; #else cout << "Do false actions" << endl;

我正在尝试hiper优化一段代码,我发现如果我将一个操作分为两部分,以防程序员希望以true或false的形式运行该方法,那么程序运行速度将提高10%,因此我尝试这样做:

template<bool way> void action(){
    #if way
    cout << "Do true actions" << endl;
    #else
    cout << "Do false actions" << endl;
    #endif // way
}

int main()
{
    action<true>();
    action<false>();
    return 0;
}
当然,这样做的一种方法是执行两个不同的函数,但在我的代码中,这将显著增加一个布尔值的函数数。 那么,我该怎么做呢?(将模板的参数传递给预处理器#if)

如何使预处理器获取模板参数

对不起,不行,你不能那样做

那么,我该怎么做呢

P>可选地,C++给了你一个精确的调用方法,它可以让你在编译时对代码进行评估,比如:

template<bool way>
std::enable_if_t<way> action()
{
    std::cout << "Do true actions\n";
} 

template<bool way>
std::enable_if_t<!way> action()
{
    std::cout << "Do false actions\n";
} 

int main()
{
    action<true>();
    action<false>();
}

混合预处理器和C++通常是行不通的。 您可以引入一个带有bool参数的模板,然后专门处理true和false

#include <iostream>

using namespace std;

template<bool way> void action(void)
{
    cout << "neither true nor false" << endl;
    // does not get called, not even for "2",
    // because bool ensures false or true
}

template<> void action<false>(void)
{
    cout << "false" << endl;
}

template<> void action<true>(void)
{
    cout << "true" << endl;
}

int main()
{
    action<false>();
    action<true>();
    action<2>(); // true, forced by using bool instead of int as parameter
    return 0;
}
#包括
使用名称空间std;
模板作废操作(作废)
{

C++ C++ C++ 17不可能使用这种方法,在C++中,C++可以使用<代码>如果CONTXPRPR 实现相同的结果,那么你需要一个支持C++ 17的现代C++编译器。它也将有助于读更多的C++书籍,特别是解释预处理器是什么的章节以及它是怎样的。工作,特别是源代码在任何其他内容之前都经过预处理,因此预处理器对任何模板、函数或代码都没有任何线索。A
if constexpr
当然!谢谢!如果演示的代码没有公共部分,为什么要避免使用两个函数?您能否更改示例以演示在一个函数?
模板void action(){
只想通过对真/假的单独专门化来解决。我实际上有这样的答案,但现在想了解一个函数的要求。一个函数有两个专门化是可以接受的解决方案吗?有趣的解决方案
template<bool way>
void action()
{
    if constexpr (way)
    {
        std::cout << "Do true actions\n";
    }
    else
    {
        std::cout << "Do false actions\n";
    }
} 
#include <iostream>

using namespace std;

template<bool way> void action(void)
{
    cout << "neither true nor false" << endl;
    // does not get called, not even for "2",
    // because bool ensures false or true
}

template<> void action<false>(void)
{
    cout << "false" << endl;
}

template<> void action<true>(void)
{
    cout << "true" << endl;
}

int main()
{
    action<false>();
    action<true>();
    action<2>(); // true, forced by using bool instead of int as parameter
    return 0;
}