dimanche 28 juillet 2019

parameter-pack-like usages acted wired

I want to have a function with a variadic parameter number, there are two ways to achieve this - parameter pack from C++11 - va_list from C lang I think. The C way is not good, because it has no type information provided

official parameter pack is something used in template classes/functions, but it is compilable when being outside of a template, which acted wired, what's the right way to achieve this?

#include<iostream>

class A {
 public:
  int a;
  int P(A* args ...) {
    for(A *m: { args  }) {
      std::cout << m->a << " ";
    }
  }
};


int main() {
  A AA[10];
  for(int i = 0; i < 10; i++) {
    AA[i].a = i+1;
  }
  AA[0].P(AA+1, AA+2, AA+3, AA+4);
}


This c++11 code printf 2, which is far away from what I expected to be 2 3 4 5, why?

Aucun commentaire:

Enregistrer un commentaire