Variable Length Argument Lists -- A kind of template?

T

Tomás

Let's say we have a variable length argument list function like so:

unsigned GetAverage(unsigned a, unsigned b, ...);

I wonder does the compiler go through the code looking for invocations of
the function, and from there, treat it sort of like a template function?
For instance, if we had a template function like so:

template<class T>
T GetAverage(T a, T b);

Then the compiler would go through the code, and where ever this template
function is invoked, it "makes a new function" with the types specified
by the code.

Yes I know compilers may do things however they please (for the most
part), but I wonder if v-l-a-l functions are treated like template
functions?

-Tomás
 
V

Victor Bazarov

Tomás said:
Let's say we have a variable length argument list function like so:

unsigned GetAverage(unsigned a, unsigned b, ...);

I wonder does the compiler go through the code looking for invocations of
the function, and from there, treat it sort of like a template function?
No.

For instance, if we had a template function like so:

template<class T>
T GetAverage(T a, T b);

Then the compiler would go through the code, and where ever this template
function is invoked, it "makes a new function" with the types specified
by the code.

It generates code that would invoke a particular instantiation of the
template. That instantiation will be merged with all other instantiations
with the same template arguments. So, "makes a new function" is not true
in all senses.
Yes I know compilers may do things however they please (for the most
part), but I wonder if v-l-a-l functions are treated like template
functions?

No, they are not.

V
 
J

Jerry Coffin

Let's say we have a variable length argument list function like so:

unsigned GetAverage(unsigned a, unsigned b, ...);

I wonder does the compiler go through the code looking for invocations of
the function, and from there, treat it sort of like a template function?

No -- not even close.
For instance, if we had a template function like so:

template<class T>
T GetAverage(T a, T b);

Then the compiler would go through the code, and where ever this template
function is invoked, it "makes a new function" with the types specified
by the code.

Yes I know compilers may do things however they please (for the most
part), but I wonder if v-l-a-l functions are treated like template
functions?

No. They're treated as functions with type-checking
turned off (except for the fixed arguments). Consider a
call to printf:

printf("A number: %d, and a string: %s", x, y);

The format string is a fixed argument, so it's treated
like it would with any other function call.

For the remaining arguments, the compiler takes whatever
you've supplied and generates code to:
1) do a default promotion on it if necessary
2) push the promoted value onto the stack

As it's doing so, it keeps track of the number of items
pushed onto the stack, and generates code to clear that
much off the stack after the function has returned.

The lack of type checking/matching is part of why you
can't (for example) combine function overloading with
variable argument lists. The compiler also needs
intrinsic knowledge of the type being passed, so you can
only pass POD types (at least with defined results).
 
G

Greg

Tomás said:
Let's say we have a variable length argument list function like so:

unsigned GetAverage(unsigned a, unsigned b, ...);

I wonder does the compiler go through the code looking for invocations of
the function, and from there, treat it sort of like a template function?
For instance, if we had a template function like so:

template<class T>
T GetAverage(T a, T b);

Then the compiler would go through the code, and where ever this template
function is invoked, it "makes a new function" with the types specified
by the code.

Yes I know compilers may do things however they please (for the most
part), but I wonder if v-l-a-l functions are treated like template
functions?

You can use a std::tr1::tuple and a function template in much the way
you envision a variable argument list working.

The template function code will be able to count the number of
variables passed in the tuple as well to ascertain their individual
types and values.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top