英文:
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 <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;
}
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 <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);
}
// or call BBB by string value
mapped_functions.at("BBB")(1);
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论