Variable Length Argument Lists -- A kind of template?

Discussion in 'C++' started by Tomás, Mar 8, 2006.

  1. Tomás

    Tomás Guest

    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
    Tomás, Mar 8, 2006
    #1
    1. Advertising

  2. Tomás wrote:
    > 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
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Mar 8, 2006
    #2
    1. Advertising

  3. Tomás

    Jerry Coffin Guest

    In article <tyGPf.6963$>,
    says...
    > 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).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Mar 8, 2006
    #3
  4. Tomás

    Greg Guest

    Tomás wrote:
    > 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
    Greg, Mar 9, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    618
    Eric Enright
    Nov 15, 2004
  2. Jonathan Burd

    Help with Variable-Length Argument Lists

    Jonathan Burd, Dec 14, 2004, in forum: C Programming
    Replies:
    5
    Views:
    347
    Keith Thompson
    Dec 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,856
    Eric Sosman
    Jan 7, 2005
  4. nw
    Replies:
    0
    Views:
    296
  5. Spencer Pearson

    Tuples vs. variable-length argument lists

    Spencer Pearson, Mar 20, 2010, in forum: Python
    Replies:
    2
    Views:
    454
    Jean-Michel Pichavant
    Mar 22, 2010
Loading...

Share This Page