Home Use pack parameter in template vector & Lambda - c++
Reply: 1

Use pack parameter in template vector & Lambda - c++

nodeover
1#
nodeover Published in 2018-02-10 22:06:45Z

I am trying variadic template pack inside lambda function with vector. Learning the c++ programming langage book, it's said "If you need to capture a variadic template argument, use ..." (with a simple example).

I am not able to compile it, my aim is just to be able to print the variadic type inside my lambda:

#include <iostream>
#include <vector>
#include <typeinfo>

using namespace std;

template<typename... vectType>
void printAllElements(vector<vectType...>& nbList, ostream& output, int nbToTest){
  for_each(begin(nbList),end(nbList),
    [&vectType...](vectType... x){
      vectType... v;
      output << x << endl;
      output << typeid(v).name() << endl;
    }
  );
}

int main() {
  vector<int> myVect {10,20,156,236};
  printAllElements(myVect,cout, 4);
}

StackTrace:

learnC++/main.cpp:10:7: error: 'vectType' in capture list does not name a variable
    [&vectType...](vectType... x){
      ^
learnC++/main.cpp:11:15: error: only function and template parameters can be parameter packs
      vectType... v;
              ^
learnC++/main.cpp:12:7: error: variable 'output' cannot be implicitly captured in a lambda with no capture-default specified
      output << x << endl;

Regards

Update 1:

Ok so I update this question to add the two code of Stroustrup I concataned with his advices.

Lambda & Vector

Void print_modulo(const vector<int>& v, ostream& os, int m){
  for_each(begin(b),end(v),
  [&os,m](int x){ if (x%m==0 os << w << '\n');}
}

The version using lambda is a clear winner. But if you really want a name, you can just provide one ..

  1. Capture template

If you need to capture template argument, use ... For example:

template<typename... Var>
void algo(int s, Var... v){
  auto helper = [&s,&v...]{return s*(h1(v...)+h2(v...);)};
}

h1 & h2 are simple examples functions

liliscent
2#
liliscent Reply to 2018-02-11 19:15:14Z

I will try to explain what is wrong in your code, but to be truly understood, you need to read some good books.

  • std::vector is a homogeneous container, all its elements have only one same type. This vector<vectType...> is pointless. For example, std::vector<int> has all int elements; there is no such a thing std::vector<int, double, short>. Thus if you want to print the typeid for some different types, your approach is wrong from the beginning.

  • In your own code [&vectType...], those vectType are types, that's also pointless. In your quoted code [&s,&v...], those v are variables, thus correct.

  • Such vectType... v; declaration is invented by yourself, it's not C++.

The following is a simple example in C++14 if you want to print variables' typeid in a generic way:

#include <iostream>

template<class... Args>
void print_all_typeid(Args const &...args)
{
    auto dumb = [] (auto... x) {};
    auto printer = [] (auto const &x) {
        std::cout << typeid(x).name() << "\n";
        return true;
    };

    dumb(printer(args)...);
}

int main() {
    print_all_typeid(std::cin, 12, std::cout, 1L, 2.0, 3.f);
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307432 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO