Author Topic: [C++] Functions with undefined number of parameters.  (Read 7217 times)

0 Members and 1 Guest are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
I recently needed to make a function with a variable amount of parameters.

To my surprise it turns out that you can indeed do such a thing in C++.
http://www.go4expert.com/forums/showthread.php?t=17592

How good/bad of an idea is this? Is this "feature" well known enough to include it in publishable code?

Essentially I'm making a vector font and I'd like to feed each Letter object an undefined number of Point objects in a single constructor.
« Last Edit: May 21, 2011 by Pixel_Outlaw »
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Thats an interesting find, since it's using the stdarg part of the standard library then I would say its a feature you can use, since the standard library is considered part of the languages ISO standard. Tho I must admit I never encoutered this one before, but I have never really delved into all the standard lib stuff beyond what I needed.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Not a problem if you use it right.  This same feature even made it into C#.
Jim
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
The stdarg stuff is ok. Maybe a bit too much C, and not C++ design but well, if you don't mind it's ok. Just for your knowledge, printf / scanf (and all derivated) are using stdargs.
Maybe you can replace it, with a std::vector, or a template ... but it depends of your code :D
The demoscene will never die, never!

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Please correct my understanding if I'm wrong...

What the author does not mention is that you can only pass Plain Old Data into functions which utalize "..." or the ellipsis.

I tried to make a constructor for a wire frame "Letter" object that would take an unlimited number of Point objects and the compiler refused to build it.


Code: [Select]
Letter(Point pt, ...)
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Hum ... I am still really wondering why not using a list of Points ... looks like it's the same ...
The demoscene will never die, never!

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
I probably would have done that if you could initialize a known series of elements in a list in a single line.
...or can one?

See the problem is calling around 8 "push backs". It gets very ugly when you have to construct 40 letters for a sroller.
There is a solution here where the author parses string data with "," delimters between the string letters.

I just threw my hands in the air and went with fixed size arrays (since arrays can be initialized with data in a  single line)
http://www.bdsoft.com/tools/initutil.html
Challenge Trophies Won:

Offline cybro

  • ZX 81
  • *
  • Posts: 3
  • Karma: 0
  • digikomm
    • View Profile
Re: [C++] Functions with undefined number of parameters.
« Reply #7 on: February 13, 2012 »
Maybe you've already made up your mind, but just one thing worth mentioning:

Although you can use this like how printf() does, you can have it be NULL delimited too.

As in all on one line:

Code: [Select]
myfunc(ARG1, ARG2, ARG3, .... ARGn, NULL);
And inside myfunc:

Code: [Select]
#include <stdio.h>
#include <stdarg.h>
void myFunc(void *FirstObject, ...)
{
void* object=FirstObject;
va_list ap;
va_start(ap, FirstObject);
/*--get it started--*/
do
{
/*--do your thing with object--*/
printf("%s ",object);
object = va_arg(ap,void*);
} while(object!=NULL);
va_end(ap);
}

void myFunc2(unsigned firstNum, ...)
{
unsigned num=firstNum;
va_list ap;
va_start(ap, firstNum);
/*--get it started--*/
do
{
/*--do your thing with object--*/
printf("%d ",num);
num = va_arg(ap,unsigned);
} while(num!=0xDEADBEEF);
va_end(ap);
}

int main(int argc, char *argv[])
{
    myFunc("Hello, world!\n","what's up?","yoyoyo",NULL);
myFunc2(1,2,3,4,5,6,7,8,0xDEADBEEF);
    return 0;
}



In C++ there is probably someway to make a linked list class and overload operators so you can use << or + or something, so you could have a line like:

myfunc << arg1 << arg2 << arg3;

or myfunc + arg1 + arg2 + arg3;

But I don't know enough about C++ to know if that is true or what the pitfalls might be.
« Last Edit: February 13, 2012 by cybro »

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] Functions with undefined number of parameters.
« Reply #8 on: February 13, 2012 »
Quote from: cybro
myfunc << arg1 << arg2 << arg3;
And C++11 offers initializer lists.
Challenge Trophies Won: