1

I have been searching for this in stackoverflow but i was unable to understand most of the solutions proposed. The problem is that i need to write a function that may accept any datatype and the number of arguments to the function also vary..For now i would just like to print those arguments in the function.

void printVariableArgs(){
//print all variables that are passed to this function
}

Is it possible to do this in c++, If so how is it done?

4 Answers 4

2

C++11 has variadic templates.

Here's an example of printf using them:

void printf(const char *s)
{
    while (*s) {
        if (*s == '%' && *(++s) != '%')
            throw std::runtime_error("invalid format string: missing arguments");
        std::cout << *s++;
    }
}

template<typename T, typename... Args>
void printf(const char *s, T value, Args... args)
{
    while (*s) {
        if (*s == '%' && *(++s) != '%') {
            std::cout << value;
            ++s;
            printf(s, args...); // call even when *s == 0 to detect extra arguments
            return;
        }
        std::cout << *s++;
    }
    throw std::logic_error("extra arguments provided to printf");
}

You could always just use printf directly.

There are also stdarg.h for variable arguments:

/* print all non-negative args one at a time;
   all args are assumed to be of int type */
void printargs(int arg1, ...)
{
  va_list ap;
  int i;

  va_start(ap, arg1); 
  for (i = arg1; i >= 0; i = va_arg(ap, int))
    printf("%d ", i);
  va_end(ap);
  putchar('\n');
}
Sign up to request clarification or add additional context in comments.

1 Comment

<stdarg.h> is not a GCC extension, it is in the C & C++ standards.
0

The first argument cannot be arbitrary, and you should have some way to determine what is the types of other arguments (because e.g. double and int are passed differently and have different sizes). printf could be an inspiration (its first argument is a format string describing what are next arguments).

Then look at va_arg

It may be considered poor taste to use it, because you lose all the type checks done by the compiler.

See also Pubby's good answer about variadic templates (which you only have in C++11).

Comments

0

aaa i know that you can define a template. look this :

void T func (T x)
{
     return T-object;
}

in this case you can pass int to the function and return the int , or pass a float and the function return the float object.

but i don`t know how you can pass different number of variable to function.sorry

look at this page too http://en.wikipedia.org/wiki/Variadic_function

Comments

0

If you have some way of determining the data type of these functions, such as the use of a format string, you can create a function similar to printf.

Depending on what exactly you're trying to do, you could also create a base class and pass an array of that derived type, each overriding a print() method. You can apparently also have Variadic functions in the new C++ standard, however I haven't ever used them before.

Even with the above two options, you can't cater for every data type. Perhaps you can explain what you're trying to do and why so we can offer alternatives.

1 Comment

I really do not know what the variable types would be..At the most i can pass a vector that stores the type of the variables..

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.