C++ 在C2664(VC 12.0;C+;和#x2B;11)中使用值的映射放置具有私有构造函数结果
以下代码在Visual Studio 2015(VC 14.0;C++14)中编译无误: 我现在明白了为什么VC12.0的例子失败了,但是在最初的(VC14.0)例子中,标准的哪一部分被用来编译呢C++ 在C2664(VC 12.0;C+;和#x2B;11)中使用值的映射放置具有私有构造函数结果,c++,stdmap,std-pair,emplace,C++,Stdmap,Std Pair,Emplace,以下代码在Visual Studio 2015(VC 14.0;C++14)中编译无误: 我现在明白了为什么VC12.0的例子失败了,但是在最初的(VC14.0)例子中,标准的哪一部分被用来编译呢 表示Visual Studio 2015对C++ 14/17有“改进的支持”: #include <string> #include <map> #include <memory> class Caller; class SomeClass { private:
表示Visual Studio 2015对C++ 14/17有“改进的支持”:
#include <string>
#include <map>
#include <memory>
class Caller;
class SomeClass
{
private:
friend class Caller;
SomeClass(std::string& arg1, std::string& arg2)
:
m_arg1(arg1),
m_arg2(arg2)
{ }
std::string m_arg1;
std::string m_arg2;
};
typedef std::shared_ptr<SomeClass> SomeClassPtr;
class Caller
{
public:
void PopulateMap()
{
std::string key("test");
m_map.emplace(key, new SomeClass { std::string("1"), std::string("2") });
}
private:
std::map<std::string, SomeClassPtr> m_map;
};
int main()
{
Caller caller;
caller.PopulateMap();
return 0;
}
c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600): error C2664: 'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)' : cannot convert argument 2 from 'SomeClass *' to 'const SomeClassPtr &'
1> with
1> [
1> _Kty=std::string
1> , _Ty=SomeClassPtr
1> ]
1> Reason: cannot convert from 'SomeClass *' to 'const SomeClassPtr'
1> Constructor for class 'std::shared_ptr<SomeClass>' is declared 'explicit'
...
m_map.emplace(std::piecewise_construct,
std::forward_as_tuple(key),
std::forward_as_tuple(new SomeClass(std::string("1"), std::string("2"))));