Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Variable Number of Arguments with different type - C++

Tags:

c++

I am coding in C++ and have a few questions regarding the ellipsis:

  1. Is it possible to pass in class or class pointer into the ellipsis?

  2. Basically what I want to do is to pass in variable number of arguments in the type of char* and class. I am using ellipsis currently and trying to figure out how to pass in the class. If ellipsis isn't applicable over here, what are the options available?

I want to let the user to directly call the function using func( params 1, params2, ...) without explicitly assigning the params into a vector or array first before passing the vector or array as argument to the function.

like image 841
Steveng Avatar asked Aug 24 '10 10:08

Steveng


People also ask

How do you pass variable number parameters in C?

Parameters in C functions There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

How many maximum arguments can be passed to a function in C?

The maximum number of arguments (and corresponding parameters) is 253 for a single function. Arguments are separated by commas. However, the comma is not an operator in this context, and the arguments can be evaluated by the compiler in any order.

Can we pass a variable number of arguments to a function?

Yes you can pass variable no. of arguments to a function.

What mechanism is used in C for functions with variable number of arguments?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.


2 Answers

You can pass whatever you want to variadic functions, but this won't help you in writing convenient functions as you are losing the type information for the arguments.

Depending on what you want to achieve there are better alternatives:

  • chaining operators like << or ():

    helper() << a << b << c;
    helper(a)(b)(c);
    
  • using (pseudo-)variadic templates:

    template<class T0>           void func(T0 t0)        { ... }
    template<class T0, class T1> void func(T0 t0, T1 t1) { ... }
    // ...
    
    // or if you can use C++0x features:
    template<class... Args> void func(Args... args) { ... }
    
  • ... more?
like image 39
Georg Fritzsche Avatar answered Sep 18 '22 17:09

Georg Fritzsche


You should consider that using variadic functions (C-style) is a dangerous flaw. If the objects passed to the function mismatch the type awaited, or if you don't put the exact number of parameters awaited, then you basically have a violent crash at runtime.

In Bjarne Stroustrup C++ In Depth Series - C++ Coding Standards - 101 Rules, Guidelines, And Best Practices by Herb Sutter and Andrei Alexandrescu, chapter 98: Don't use varargs (ellipsis)

I deeply subscribe to @tenfour's proposal:

  • use an std::vector that contains all your parameters.
like image 157
Stephane Rolland Avatar answered Sep 17 '22 17:09

Stephane Rolland