简体   繁体   中英

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) ?

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.

Though ellipsis gives us some useful functionality, it is quite dangerous to use them. When using ellipsis, the compiler does not check the type of arguments passed to the function. So the compiler does not throw any error if arguments are of different types. Even if pass string, double, or bool type values are passed to the average() function it returns return an unexpected value, the compiler does not throw any error.

Source: https://www.geeksforgeeks.org/ellipsis-in-c-with-examples/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM