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?
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.
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.
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 :-)