【发布时间】:2016-12-22 01:09:10
【问题描述】:
如何初始化initializer_list 无序元素的组合?
例如,考虑以下代码:
#include <unordered_map>
#include <string>
using Type = std::unordered_map<std::string, int>;
void foo(std::initializer_list<Type> l) {}
int main()
{
Type x = {{"xxx", 0}, {"yyy", 1}}; // compile fine
// std::initializer_list<Type> y = {{"xxx", 0}, {"yyy", 1}};
// foo({{"xxx", 0}, {"yyy", 1}});
}
如果我取消注释第一行,我得到 (g++ 6.1.1) 错误:
错误:无法将'{{"xxx", 0}, {"yyy", 1}}'从''转换为'std::initializer_list, int> >' std::initializer_list y = {{"xxx", 0}, {"yyy", 1}};
最后,我想做的是能够像第二条注释行一样调用foo,这也无法编译。
【问题讨论】:
-
foo({{{"xxx", 0}, {"yyy", 1}}});