Search code examples
c++variadic-functionsellipsis

Can variables be used in function call in ellipsis functions in C++


For this function that takes variable number of arguments,

void func(int count, ...)  // ellipsis function
{
// function definition
}

Can a function call be made like follows :

int a{};
double b{};
string c{};

func(3,a,b,c); // using actual variables instead of fixed values in function call

My question is when an ellipsis function is called does it always has to be just fixed values like func(3,5,2.7,"Hi") or can variables be supplied in the function call like so func(3,a,b,c)?


Solution

  • Note that passing classes like std::string, with non-trivial copy constructor or nontrivial move constructor or non-trivial destructor, may not be supported and has "implementation-defined" semantics. You have to check your compiler documentation on how such classes are passed or check if they are supported at all.

    Can variables be used in function call in ellipsis functions in C++

    Yes.

    Can a function call be made like follows

    Yes.

    when an ellipsis function is called does it always has to be just fixed values like func(3,5,2.7,"Hi")

    No.

    can variables be supplied in the function call like so func(3,a,b,c)?

    Yes.

    Can you suggest any reference so I can do some research on it?

    https://en.cppreference.com/w/cpp/language/variadic_arguments https://en.cppreference.com/w/cpp/utility/variadic https://eel.is/c++draft/expr#call-12

    And in C++ you should strongly prefer: https://en.cppreference.com/w/cpp/language/parameter_pack , because of type safety.