-2

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

nodeover
  • 301
  • 1
  • 2
  • 11
  • 3
    Sorry... not to discourage you... but your code is wrong is so many way that I don't know how to start explaining it. Suggestion: try to understand variadic templates starting with very simple examples. – max66 Feb 10 '18 at 22:26
  • 1
    As the comment above mentioned, your use of `std::vector`, lambda capturing, variable declaration are all wrong. – llllllllll Feb 10 '18 at 22:37
  • Thanks for you comments, I added the two examples of Stroustrup in his book, In fact I am learning c++, So I try my way too. I just wanted to concat its two examples to test myself. Can you tell me what is really wrong ? I did not think I was so far from the true; and I sure want to understand my faults – nodeover Feb 11 '18 at 09:14
  • Is somebody could help me a little? @liliscent you said all is wrong, I really do not understand what you mean, you pseak about the pack parameter? – nodeover Feb 11 '18 at 18:12

1 Answers1

1

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);
}
llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • Thanks for your advices, as said in my post I am reading the "c++ programming langage" from Stroustrup. So I think it's a good book (1300 pages ouch) I am not a beginner developper, the book is very good, I read It from start to end but sometimes there are things (like variadic template) I do not know that are juste showed before explained 300 pages latter; I think that is my misunderstand came from that fact. – nodeover Feb 12 '18 at 09:30