如何在嵌套列表初始化中从map<><>中辨别向量?
How to discern vector<> from map<> in nested list initialization?
考虑一个定义为:
class C {
public:
C(std::vector<std::string>) {}; // (1)
C(std::map<std::string, std::string>) {}; // (2)
};
然后是它的两个实例,使用列表初始值设定项创建,如下所示:
C A({ "A", "B" });
C B({ { "A", "B" }, { "C", "D" } });
我希望对象A
使用(1)
构造函数和对象B
经历(2)
。但事实并非如此,我得到以下输出:error: call to constructor of 'C' is ambiguous
.
此外,如果我尝试声明这些实例,例如:
C A = { "A", "B" };
C B = { { "A", "B" }, { "C", "D" } };
它失败了:error: no matching constructor for initialization of 'C'
.
这是怎么回事?
我怎样才能达到预期的行为?( A -> (1)
, B -> (2)
(也许以某种方式使用SFINAE?
我通过以下方式实现了您想要的:
class C {
public:
C(std::vector<std::string>) { std::cout << "here1" << std::endl; } // (1)
C(std::map<std::string, std::string>) { std::cout << "here2" << std::endl; } // (2)
};
int main()
{
C A = C( { {"A"}, {"B"} });
C B = C( std::map<std::string, std::string> { {"A", "B"}, {"C", "D"} } );
// or C B = C( { std::make_pair("A", "B"), std::make_pair("C", "D") } );
}
已编辑(使用统一初始化(
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中