是否可以使用初始值设定项列表创建临时结构/类
Is it possible to create temp structs/classes with initializer lists?
这看起来是一个非常基本的问题,应该有一个明确的谷歌答案,但到目前为止我还没能找到。答案似乎是否定的,但我想确定一下。我的意思是这样的东西:
struct A { int a,b; };
void foo(const A&); // just some function
foo(A{1,2}); // doesn't work, but it was a long shot
foo(A({1,2})); // looks like it could have worked, but it doesn't
foo(A(std::initializer_list<int>({1,2}))); // ugly and doesn't work either anyway
更新:选项2实际上有效(以及一个更简单的foo({1,2})),但仅在vs2013中有效,而在2012中无效。
更新#2:拜托,有必要这么努力地否决它吗?知道它只从vs2013开始工作仍然很有用。
甚至比这更容易。看看统一初始化的力量:
foo({1,2});
相关文章:
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C++ 双向链表:使用矩阵类成员创建和填充列表
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 使用 SWI-cpp.h 创建列表
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- 动态分配列表 - 创建一个函数,用于删除所有包含偶数值的元素
- 基于数组的列表 - 动态数组创建时出错
- 如何从一组派生类创建列表?
- 创建列表视图后无法更改列表视图中的列宽
- 如何在两个包含指针的地图中创建列表
- 在 C++ 中创建列表列表
- 在QT中启动程序时如何创建列表或对象
- 创建列表中的第一个元素始终是0
- 根据网络消息c++创建列表
- C++从两个迭代器创建列表对
- <int> 使用 STIL 创建列表
- 使用 C++ QT 创建列表视图
- 在 MFC 中创建列表视图,而无需对话框
- 在boost python中创建列表列表