This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: function call argument count


Of course using a vector, map, etc. only works if all the parameters are of
the same type. Using the explicit count is a better option for different
argument types (if the type order is known).

I sometimes use some defined value before each variable to say there is
another parameter and to give a clue to the type, and then use a different
value to define the end of the list. These can be #defines or an enumerated
type. For example:

enum Var_Types
{
    End = 0, Int, Double, String
};

void myFunc(Var_Types more,...);

Then:

myFunc(End);

would mean no parameters and

myFunc(Int, 1, Int, 3, Double, 5.8, String, "Hello", String, "Goodbye",
End);

You then test the Var_Types value, and the next parameter according to it.
You could use something similar for a parameter list with all the same type
if you have a value you know won't be passed in and can use it as a
terminator - e.g. expecting positive integers could end with a value of -1.

This was just in case you're interested in lists with different types... :-)

Chris

----- Original Message ----- 
From: "Eljay Love-Jensen" <eljay@adobe.com>
To: "don fisher" <dfisher@as.arizona.edu>; <gcc-help@gcc.gnu.org>
Sent: Monday, May 24, 2004 1:30 PM
Subject: Re: function call argument count


> Hi Don,
>
> There's not an easy way that is platform agnostic to determine the
argument
> count.  You can do it yourself as a convention:
>
> void MyFunc(int argcount, ...);
>
> Then call it with the explicit argument count, via:
>
> int a=1, b=2, c=3;
> MyFunc(3, a, b, c);
>
> I recommend that variable arguments be passed in as a std::vector<> or
> std::map<> or std::set<> or whatever kind of container is appropriate.  Or
> better yet, as generic begin() and end() iterators to the aforementioned
> containers.  By "generic" I mean using templates.
>
> HTH,
> --Eljay
>
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]