c++ - 如何使用可变数量的参数获取宏的值?

标签 c++

我尝试获取宏中参数的每个值,如下所示

#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::vectorstd::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/

相关文章:

c++ - pcre 不能支持多个子组

C++ 大括号初始值设定项作为参数调用与预期不同的构造函数

c++ - 删除映射中的指针会导致内存错误

c++ - 如何创建共享概念的对象 vector ?

c++ - 关于 MinGW32 的复制构造函数的疯狂 C++ 编译器错误消息

C++ bool 运算符==

c++ - 在 LLVM 中调用 CreatePHI() 时出错

c++ - 打开后如何聚焦opencv窗口?

c++ - 使用命名管道 WinAPI 的异步 I/O

c++ - 安卓NDK : gnuSTL_static library documentation?