简体   繁体   English

变量可以用于 function 调用中的省略号函数 C++

[英]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.

相关问题 C++ 编译器能否优化用于将函数结果传递给另一个函数的虚拟变量的使用? - Can the C++ compiler optimize away the use of dummy variables that are used to pass the results of functions to another function? C ++函数 - 错误:'0'不能用作函数 - C++ Functions - Error: '0' cannot be used as a function 运营商可以用作功能吗? (C ++) - Can operators be used as functions? (C++) 为什么*这是从const成员函数返回的,不能用于在C ++中调用其他函数? - Why *this returned from a const member function cannot be used to call other functions in C++? 获取变量以调用函数c ++ - getting variables to call for a function c++ 有人可以解释一下在C ++函数调用中如何使用c字符串吗? - Can someone please explain how c-strings are used in a C++ function call? 使用main中的变量和函数调用C ++中包含的类中的方法 - Call to methods contained in class in C++ with variables and functions in the main C ++中的attribute-list中的省略号应该用于什么? - What should ellipsis in attribute-list in C++ be used for? 可以在C ++中使用C标准库中的函数吗? - Can functions from the C standard library be used in C++? Static function 无法访问或使用 C++ - Static function can not be accessed or used C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM