Rob*_*rtS 3 c++ function ellipsis variadic variadic-functions
I\xc2\xb4ve 遇到了一个函数声明,例如:
\n\nint vsa_d(...);\nRun Code Online (Sandbox Code Playgroud)\n\n和...一个且唯一的参数。
我知道通过省略号,我们可以指代多个对象,但是指代的是什么?...这里指的是什么呢?
这意味着什么以及它的目的是什么?
到什么...编译器评估
在调用函数时,省略号也可以用作函数参数吗?
我\xc2\xb4ve在“注释”下找到了https://en.cppreference.com/w/cpp/language/variadic_arguments:
\n\n\n\n\n在C编程语言中,省略号参数之前必须至少出现一个命名参数,因此 printz(...); 无效。在 C++ 中,即使传递给此类函数的参数不可访问,也允许这种形式,并且通常用作 SFINAE 中的后备重载,利用重载决策中省略号转换的最低优先级。
\n
因此,它应用于“ SFINAE”中的“后备过载”之类的情况”之类的情况。
\n\n这意味着什么?
\n该...参数在某些 SFINAE 结构中用作包罗万象的内容。
以下是有关编写检测类型 T 是否具有成员的类型特征的问题中的最佳答案的例外:has_helloworld<T>helloworld
template <typename T>
class has_helloworld
{
typedef char one;
struct two { char x[2]; };
template <typename C> static one test( typeof(&C::helloworld) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
int main(int argc, char *argv[])
{
std::cout << has_helloworld<Hello>::value << std::endl;
std::cout << has_helloworld<Generic>::value << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它的工作方式如下:如果typeof(&T::helloworld)存在并且格式良好,则在 site 处test<T>(0),常量0将转换为指向成员(函数)的指针,并选择该重载。返回类型的大小为一。
如果typeof(&T::helloworld) 不存在,则该过载不在潜在过载集中,并且test(...)选择回退作为过载。返回类型的大小是二。
重载test(...)有一个很好的特性,即它始终是最差匹配的、最后选择的重载。这意味着它可以充当此类结构中的“后备默认值”。