我尝试获取宏中参数的每个值,如下所示
#include <iostream>
#include <stdio.h>
#include <tuple>
using namespace std;
class T {
public:
string a;
string b;
};
#define CONFIG_FUNCTION(...) int SetValue(T t){\
int arg_len = tuple_size<decltype(make_tuple(__VA_ARGS__))>::value;\
auto t = make_tuple(__VA_ARGS__);\
int i = 0;\
cout << arg_len << endl;\
while (i < arg_len) {\
// I need to get every value of __VA_ARGS__
// t.a = "assigntment"
}\
cout << get<1>(t) << endl;\
}
CONFIG_FUNCTION("a", "b", "c", "d", "e");
int main()
{
T t;
SetValue(t);
return 0;
}
参数个数(“a”、“b”、“c”、“d”、“e”)是可变的,如何遍历值。
最佳答案
The number of arguments ("a", "b", "c", "d", "e") are variable, how can I traverse the value.
似乎使用 std::tuple
(或封装它的宏)是执行此操作的错误方法(无论您实际上想做什么)。
如果您有未知数量的相同类型的参数,您可以简单地使用适当的 std::vector
和 std::initializer_list
,例如
std::vector<std::string> v1{"a", "b", "c", "d", "e"};
for(auto s : v1) {
// Handle every value contained in v1
}
std::vector<std::string> v2{"a", "b", "c", "d", "e", "f", "g"};
for(auto s : v2) {
// Handle every value contained in v2
}
关于c++ - 如何使用可变数量的参数获取宏的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048252/