使用宏来连接函数名

huangapple go评论61阅读模式
英文:

Use macro to concate function name

问题

#include <iostream>
#include <vector>
#define _SET_ENV_VAL_(F,v) set_##F##Env(v)

void setAAAEnv(int val) {
    
}
void setBBBEnv(int val) {
    
}
void setCCCEnv(int val) {
    
}
int main() {
    // Write C++ code here
    std::cout << "Hello world!";
    std::vector<std::string> funcs {"AAA", "BBB", "CCC"};
    
    for(auto iter:funcs) {
        _SET_ENV_VAL_(iter, 1);
    }
    
    
    return 0;
}

我想使用宏函数“SET_ENV_VAL”来连接函数名,但我遇到了以下编译错误。

/tmp/gjdChSSJ2K.cpp:4:41: error: ‘set_iterEnv’ was not declared in
this scope
4 | #define WX_SET_ENV_VAL(F,v) set_##F##Env(v)
| ^~~~ /tmp/gjdChSSJ2K.cpp:21:9: note: in expansion of macro
WX_SET_ENV_VAL’ 21 | WX_SET_ENV_VAL(iter, 1);
|

如何修复此错误,使宏函数按预期工作?

英文:
#include &lt;iostream&gt;
#include &lt;vector&gt;
#define _SET_ENV_VAL_(F,v) set_##F##Env(v)

void setAAAEnv(int val) {
    
}
void setBBBEnv(int val) {
    
}
void setCCCEnv(int val) {
    
}
int main() {
    // Write C++ code here
    std::cout &lt;&lt; &quot;Hello world!&quot;;
    std::vector&lt;std::string&gt; funcs {&quot;AAA&quot;, &quot;BBB&quot;, &quot;CCC&quot;};
    
    for(auto iter:funcs) {
        _SET_ENV_VAL_(iter, 1);
    }
    
    
    return 0;
}

I want to use the macro function "SET_ENV_VAL" to concate the function name, but I get some compile errors like below.

> /tmp/gjdChSSJ2K.cpp:4:41: error: ‘set_iterEnv’ was not declared in
> this scope
> 4 | #define WX_SET_ENV_VAL(F,v) set_##F##Env(v)
> | ^~~~ /tmp/gjdChSSJ2K.cpp:21:9: note: in expansion of macro
> ‘WX_SET_ENV_VAL’ 21 | WX_SET_ENV_VAL(iter, 1);
> |

How to fix this error and make the macro function working as expected?

答案1

得分: 2

在基本层面上,您混合了运行时和编译时的概念,这无论如何都不会起作用。其次,在C++中尽量避免使用宏,除非没有其他选择。

如果您只想迭代一组已知的函数,那么您的代码可以如下所示:
或者,正如某位程序员提到的,将函数放入映射中:

#include <iostream>
#include <vector>
#include <functional>
#include <map>

void setAAAEnv(int val)
{

}

void setBBBEnv(int val)
{

}

void setCCCEnv(int val)
{

}

int main()
{
    std::vector<std::function<void(int)>> functions{ setAAAEnv, setBBBEnv, setCCCEnv };

    std::map<std::string, std::function<void(int)>> mapped_functions
    {
        { "AAA", setAAAEnv },
        { "BBB", setBBBEnv },
        { "CCC", setCCCEnv }
    };

    for (const auto& iter : functions)
    {
        iter(1);
    }

    // 或者按字符串值调用BBB
    mapped_functions.at("BBB")(1);

    return 0;
}
英文:

On a fundamental level you are mixing runtime with compile time concepts and that will not work anyway. Secondly try to avoid macros in C++ unless you have no other choice.

If all you want is to iterate over a set of known functions then your code could look like this :
Or as some programmer dude mentions put the functions in a map

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;functional&gt;
#include &lt;map&gt;

void setAAAEnv(int val) 
{

}

void setBBBEnv(int val) 
{

}

void setCCCEnv(int val) 
{

}

int main() 
{
    std::vector&lt;std::function&lt;void(int)&gt;&gt; functions{ setAAAEnv, setBBBEnv, setCCCEnv };

    std::map&lt;std::string, std::function&lt;void(int)&gt;&gt; mapped_functions
    {
        { &quot;AAA&quot;, setAAAEnv },
        { &quot;BBB&quot;, setBBBEnv },
        { &quot;CCC&quot;, setCCCEnv }
    };

    for (const auto&amp; iter : functions)
    {
        iter(1);
    }

    // or call BBB by string value
    mapped_functions.at(&quot;BBB&quot;)(1);

    return 0;
}

huangapple
  • 本文由 发表于 2023年6月5日 14:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定