2

I've been looking for the answer but i'm not sure, do variable argument functions get created or resolved at compile time or dynamically? For example , is it ok to take user's input at run-time and then call the function according to how many inputs were entered? Thanks

void func(int num_args, ...)
{
 ....
}
6
  • Compile time, compare assembly with gcc -S. Arguments (vararg) are processed at run time though. Commented Jun 16, 2015 at 7:49
  • 1
    How would you exactly call a function with a dynamic number of args at run-time? Pretty sure you can't do that in C++. Also, I would strongly recommend you don't use C-style variadic functions and instead use variadic templates. Commented Jun 16, 2015 at 8:00
  • with lots of if conditions or a switch-case I guess it could be called, but it wouldn't be great code Commented Jun 16, 2015 at 8:10
  • 1
    Switch cases and conditions are both compile time constructs ;) Commented Jun 16, 2015 at 8:21
  • 2
    Note that variable functions have very limited use in real world applications. This language feature is mainly just there to support the icky legacy stdio.h functions and should be avoided in C and C++ both. Type generic functions can be solved in much better and elegant ways, particularly in C++. Commented Jun 16, 2015 at 8:25

3 Answers 3

2

The number and types of function arguments are resolved at compile time, the values are resolved at compile-time or run-time depending on whether their value is constexpr-like or not.

Think about how you would call a function with an arbitrary number of variables collected at runtime. There is no C++ syntax for such a construct.

What you should do instead is use something like std::vector. An example with some dummy functions:

void func (const std::vector<std::string>& args);
std::string getarg();
bool haveargs();

int main() {
    std::vector<std::string> args;
    while (haveargs()) {
        args.push_back(getarg());
    }
    func(args);
}
Sign up to request clarification or add additional context in comments.

3 Comments

The type of function arguments are resolved at compile time? In that case, what is the type of the variadic arguments of printf?
Could I just read in the user's input, then create many if conditions depending on the number of inputs entered and call the variable argument function according to the number of inputs entered? Messy code I know, but it should work
@Engineer999 you could, but that would be error-prone, unmaintainable and inflexible. Simply populating a std::vector or similar is a far better option.
1

do variable argument functions get created or resolved at compile time or dynamically?

C++ is not a JIT compiled language (usually), so run-time function creation isn't possible at the language level.


is it okay to take user's input at run-time and then call the function according to how many inputs were entered?

Of course, but make sure you do error checking!


All in all I would recommend against using C-style variadic functions and instead split it up into a different function for each individual argument type. Variadic functions are fraught with danger and easy to mess up.

Comments

0

They are created at compile time. Rather then passing a list of discrete parameters, you use the va_start macro to get a stack address wrapped in the returned va_list structure. The va_start macro uses the last defined argument passed to the function, so it is usual to use this to provide a mechanism to determine the actual, run time number of arguments as well as their types. For example, printf requires a format string as the other arguments may be different sizes, whereas the code below uses a simple count as it expects only integers.

int average(int count, ...)
{
    va_list valist;
    int total;
    int i;

    // Initialize valist for number of arguments specified by count.
    va_start(valist, count);

    // Get the total of the variable arguments in the va_list.
    for(total = 0, i = 0; i < num; i++)
    {
        // Use va_arg to access the next element in the va_list
        // by passing the list and the type of the next argument.
        total += va_arg(valist, int);
    }

    // Release the va_list memory.
    va_end(valist);

    return total/count;
}

// Using the function: pass the number of arguments
// then the arguments themselves.
printf("The average is %d.", average(3, 1, 2, 3));

Output: The average is 2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.