simple va_args question

Hi,
Am I missing something, or did va_args really not come with any end identifier or count information? My current understanding, and PLEASE correct me if I'm wrong, is that I have to manually count my arguments in order to know how many there are.
I mean, someone bothered to write the va_args code that contains the list of arguments, but they can't count? Seriously? Am I missing something, or do I really have to pass in an argument that I count in order to get the count of the list?
Sean
Variable argument lists have always been a tricky hack. You assume too much to say, "but they can't count?" There are, of course, ways to add counting to arguments, but they subvert part of the original intent. And how often do you really need to have variable argument lists? Chances are, whatever you are trying to do can be done better another way.

Let us know and we can suggest some things that may work better for you.

Hope this helps.
It turns out not to matter because I am dealing with classes instead of PODs.
Basically what I would like to do is write code more easily.
Say I have a vector<MyClass> and I want to initialize it as one argument in the constructor of another class. Right now, I have to make the vector, push back the pieces, etc... and then pass it in. Since those steps are all repetitive steps, I would rather just put ... in for each piece separated by commas and do the repetitive stuff once in a function. It turns out I can't even do that when I bother to count them because of the POD issue. Anyway, I think I have seen something on c++ templates with variable numbers of arguments. Any suggestions would be appreciated.
Thanks,
Sean
Last edited on
C++11's variable argument templates won't help you here. I'm not sure what else the newest version has to help you, but AFAIK you'll have to emply an intermediary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct point
  {
  int x, y;
  point(): x( 0 ), y( 0 ) { }
  point( int x, int y ): x( x ), y( y ) { }
  };

class fooey
  {
  vector <point> points;

  fooey()
    {
    point pts[] =
      {
      point( 1, 1 ),
      point( 2, 3 ),
      point( 5, 8 ),
      point( 11, 19 )
      };
    points.assign( pts, pts + 4 );
    }
  };

Hope this helps.
That's where initializer lists come in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct point
{
  int x,y;
  point() : x(0), y(0) {}
  point(int x,int y) : x(x), y(y) {}
};

struct fooey
{
  vector<point> points;
  fooey(initializer_list<point> points) : points(points) {}
};

int main()
{
  fooey foo({{1,1},{2,3},{5,8},{11,19}});
}
Awesome advice. At first I didn't quite catch it. In case anyone ends up trying to figure this out like me and didn't know what initializer_list<T> template is or how to use it, they rock and here's all I had to do to use them. Basically, I started by trying initializer_list<MyClass> like in the above example to initialize a vector. From what I managed to read somewhere, all std containers should be able to initialize with them. At first this gave me a problem of initializer_list being undefined. Thus, I tried #include<initializer_list>, but then I still had a problem. It turned out I needed to add a compiler flag too. That was simply -std=c++0x. Then everything worked like magic. In fact, it's even better than that. I then removed the initializer_list from the code and replaced it with my original code that had vector in that place, but I could still use the initializer list to initialize the vector. In other words, all you have to do is add -std=c++0x as a compiler flag and then you can initialize vectors the way you do arrays! If I understand it right, that should work for initializing any std container too. It's wonderful! I love it and I'm forever grateful for the advice :-)
Ah, yes, that is a C++11 feature, I think, to be able to use literal initializer lists like that. Sweet!
Topic archived. No new replies allowed.