473,748 Members | 10,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Length Argument Lists -- A kind of template?

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

unsigned GetAverage(unsi gned 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
Mar 8 '06 #1
3 4281
In article <ty************ ******@news.ind igo.ie>,
NU**@NULL.NULL says...
Let's say we have a variable length argument list function like so:

unsigned GetAverage(unsi gned 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).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 8 '06 #2
Tomás wrote:
Let's say we have a variable length argument list function like so:

unsigned GetAverage(unsi gned 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
--
Please remove capital As from my address when replying by mail
Mar 8 '06 #3

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

unsigned GetAverage(unsi gned 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

Mar 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1279
by: Chris Jefferson | last post by:
Now, C++ doesn't actually provide true variable-length templated classes, for example you can't do (as I found recently!) template<class T> class tuple { ... } template<class T, class U> class tuple { ... } There appear to be two main ways of "faking" this.
10
5537
by: Clay_Culver | last post by:
I have heard that it is possible to use classes + template magic to make standalone functions (or maybe just a functor which acts like a function) which can accept variable length arguments without using the elipse. For example, a funciton could take in 1 known type, then a variable list of other parameters: ReturnValueClass rvc = someFunction("Some known type", 1, 0.2, "string"); ReturnValueClass rvc2 = someFunction("Some known type...
3
1636
by: Peteris Krumins | last post by:
Hello, Is it possible to use va lists if i prefare to define functions as following: (won't compile) void die(file, line, fn, fmt, ...) const char *file;
5
1966
by: Jonathan Burd | last post by:
Greetings everyone, I wrote a function to learn about variable-length argument lists. I wonder if there is a better way to detect the end of the argument list than using a sentinel value like the one I am using (NULL in this example) or an argument count parameter (ugh). The following function concatenates a series of C-style strings. I am aware of the fact that not allocating enough memory for `dst` results in UB (gave me a...
9
2788
by: Schraalhans Keukenmeester | last post by:
I have some C functions (with variable length argument lists) that use void pointers as arguments. Is there a way to determine at runtime what type of parameter is actually passed on to the function? PHP and my oldskool turbopascal provide a typeof() function, but my C compiler (gcc 3.4.1) does not seem to provide this function. Perhaps someone crafted a library with some smart code able to inspect the variable passed to a certain...
11
2547
by: Fan Yang | last post by:
I'm reading Modern C++ Design, and it is saying "Variable template parameters simply don't exist." But I find VC7.1 & VC8 support this feature.Who can tell me that which is right -_-b Many thanks.
6
3218
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to declare a pointer to them:
9
2588
by: Chad | last post by:
This might be a bit vague and poorly worded..... In my program, I handle function failures using fprintf() and exit() like: fprintf(stderr, "malloc failed"); exit(EXIT_FAILURE); There are 5 of these. Since each one has two lines, the total lines of
19
2133
by: Spiros Bousbouras | last post by:
Every time I've seen an example of a variable argument list function its functionality was to print formatted output. Does anyone have examples where the function is not some variation of printf ?
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.