[英]Can variables be used in function call in ellipsis functions in C++
For this function that takes variable number of arguments,对于这个采用可变数量 arguments 的 function,
void func(int count, ...) // ellipsis function
{
// function definition
}
Can a function call be made like follows:可以按如下方式进行 function 调用:
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)
?我的问题是,当调用省略号 function 时,它是否总是必须是
fixed
值,如func(3,5,2.7,"Hi")
或者可以在 function 调用中提供变量,如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.请注意,像
std::string
这样的传递类,具有非平凡的复制构造函数或非平凡的移动构造函数或非平凡的析构函数,可能不受支持,并且具有“实现定义的”语义。 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++
变量可以用于 function 调用中的省略号函数 C++
Yes.是的。
Can a function call be made like follows
可以按如下方式进行 function 调用吗
Yes.是的。
when an ellipsis function is called does it always has to be just fixed values like func(3,5,2.7,"Hi")
当调用省略号 function 时,它是否总是必须是固定值,如 func(3,5,2.7,"Hi")
No.不。
can variables be supplied in the function call like so func(3,a,b,c)?
可以在 function 调用中提供变量,如 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 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.在 C++ 中,您应该强烈喜欢: https://en.cppreference.com/w/cpp/language/parameter_pack ,因为类型安全。
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.
使用省略号时,编译器不会检查传递给 function 的 arguments 的类型。 So the compiler does not throw any error if arguments are of different types.
因此,如果 arguments 属于不同类型,编译器不会抛出任何错误。 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.
即使将字符串、双精度或布尔类型的值传递给 average() function 它返回一个意外的值,编译器也不会抛出任何错误。
Source: https://www.geeksforgeeks.org/ellipsis-in-c-with-examples/资料来源: https://www.geeksforgeeks.org/ellipsis-in-c-with-examples/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.