Ellipsis作为任何可变模板之外的构造函数参数
Ellipsis as a constructor argument outside of any variadic templates
考虑以下代码:
#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;
}
相关文章:
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在按值调用 (c++) 中转发构造函数参数
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 如何在构造函数参数中初始化"std::set"?
- 何时应在构造函数参数中使用 const C++?
- 使用模板化结构作为构造函数参数
- 使用 lambda 作为构造函数参数是否需要C++ 17?
- 如何使输入文本文件成为构造函数参数?c++
- shared_ptr构造函数参数是否应按值传递
- 是否允许使用初始值设定项列表将const数组引用实例化为构造函数参数
- 复制构造函数参数为0
- 用作成员构造函数参数的函数的求值顺序
- 结构中的默认成员值或默认构造函数参数
- C++将引用成员绑定到构造函数参数
- 如何通过可变参数模板将多个构造函数参数转发到数组初始值设定项列表?
- 模板函数指针参数与构造函数参数
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 如何基于构造函数参数模板化类成员函数的代码
- 将派生类构造函数参数传递给受保护的成员
- 如何根据构造函数参数使用超类类型初始化成员变量?