Ellipsis作为任何可变模板之外的构造函数参数

Ellipsis as a constructor argument outside of any variadic templates

本文关键字:构造函数 参数 任何可 Ellipsis      更新时间:2023-10-16

考虑以下代码:

#include <iostream>
struct S
{
    S(const char *p) { std::cout << '[' << p << ']'; }
};
int main()
{
    S var(...); // <------
    return 0;
}

它使用-pedantic -pedantic-errors在GCC 5.2上编译良好,但不打印任何内容。我不知道这个语法是什么意思,也找不到任何关于它的信息

看起来它只是阻止一个对象被构造,但我从来没有听说过这样的功能。

问题是:省略号在用作构造函数参数时意味着什么

它应该是一个具有可变长度参数的函数原型。

为了确保这一点,我添加了两行,得到了未定义的引用错误。

#include <iostream>
struct S
{
    S(const char *p) {std::cout << '[' << p << ']';}
};
int main()
{
    S var(...); // <------ function prototype declaration
    var(); // attempt to call the declared function, which is not defined
    var(1); // the same as above
    return 0;
}