简体   繁体   English

遍历可变参数模板函数并选择指针参数

[英]iterate over a Variadic Template function and choose pointer arguments

I have a Variadic Template function in C++ and I want to iterate over the template arguments and cherry-pick those arguments that are pointers.我在 C++ 中有一个可变参数模板函数,我想遍历模板参数并挑选那些作为指针的参数。

PLEASE SEE THE UPDATE SECTION BELOW.请参阅下面的更新部分。

So, I have the following code below.所以,我在下面有以下代码。 I wrote a skeleton code that is compilable and runnable.我写了一个可编译和可运行的骨架代码。

g++ -std=c++17 f3_stackoverflow.cpp && ./a.out

I am just curious about the part that says: LOGIC GOES HERE我只是对上面写着的部分感到好奇: LOGIC GOES HERE

#include <iostream>
#include <vector>
#include <utility>

template <typename... Args>
std::vector<std::pair<int, void*>> check(Args... args) {
    std::vector<std::pair<int, void*>> ret;

    // LOGIC GOES HERE

    return ret;
}

void printVector(std::vector<std::pair<int, void*>> v) {
    for(const auto& _v : v) {
        std::cout << _v.first << " : " << _v.second << std::endl;
    }
}

int main(int argc, char const *argv[])
{
    int n = 100;

    int                 a;
    std::vector<int>    b(n);

    float               c;
    std::vector<float>  d(n);

    char                e;
    std::vector<char>   f(n);

    auto pairs = check(a, b.data(), c, d.data(), e, f.data());
    printVector(pairs);
    
    return 0;
}

So, I want to see the following output in the stdout of the program:所以,我想在程序的stdout输出中看到以下输出:

1 : 0x123
3 : 0x567
5 : 0x980

UPDATE:更新:

Basically, I am looking for the indexes where the argument is a pointer (eg, int* , float* , ...) and the address the pointer is pointing to.基本上,我正在寻找参数是指针的索引(例如, int*float* ,...)和指针指向的地址。 That is why you see the output that I provided.这就是为什么您会看到我提供的输出。

Explanation of the output: The second, fourth, and sixth arguments are pointers (hence, 1, 3, 5 in zero-based indexing).输出说明:第二个、第四个和第六个参数是指针(因此,在从零开始的索引中为 1、3、5)。

template <int N, typename Arg, typename... Args>
void helper(std::vector<std::pair<int, void*>>& v, Arg arg, Args... args) {
    if constexpr(std::is_pointer_v<Arg>) {
        v.emplace_back(N, (void*)arg);
    }

    if constexpr(sizeof...(args) > 0) {
        helper<N+1,  Args...>(v, args...);
    }
}

template <typename... Args>
std::vector<std::pair<int, void*>> check(Args... args) {
    std::vector<std::pair<int, void*>> ret;
    helper<0>(ret, args...);
    return ret;
}

Demo演示


Maybe a simpler version using fold expression也许是使用折叠表达式的更简单的版本

template <typename Arg>
void helper(std::vector<std::pair<int, void*>>& v, Arg arg, int idx) {
    if constexpr(std::is_pointer_v<Arg>) {
        v.emplace_back(idx, (void*)arg);
    }
}

template <typename... Args>
std::vector<std::pair<int, void*>> check(Args... args) {
    std::vector<std::pair<int, void*>> ret;
    int n = 0;
    (helper(ret, args, n++), ...);
    return ret;
}

Demo演示

There is no need to use recursion.没有必要使用递归。 In C++17, you can just combine fold-expression and immediately-invoked lambda to do this在 C++17 中,您可以结合 fold-expression 和立即调用的 lambda 来执行此操作

template <typename... Args>
std::vector<std::pair<int, void*>> check(Args... args) {
  std::vector<std::pair<int, void*>> ret;
  int i = 0;
  ([&](auto arg) {
    if constexpr (std::is_pointer_v<decltype(arg)>)
      ret.emplace_back(i, arg);
    i++;
  }(args), ...);
  return ret;
}

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM