c ++ 11/14 make_unique ambigious overload for std :: string [英] c++11/14 make_unique ambigious overload for std::string

查看:303
本文介绍了c ++ 11/14 make_unique ambigious overload for std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释如何解决的不明确的重载警告make_unique,错误来自和它的意思是什么意思(我明白什么是一个含糊的重载,但我不确定为什么我得到一个特定的代码) ?我使用c ++ 11,因此我使用推荐的模板从Herb Sutter。



使用它我得到以下错误:

 错误4错误C2668:'make_unique':重载函数的模糊调用

在visual studio 13中悬停在工具提示上给我以下方法:

  std :: enable_if<!std :: is_array< _Ty> :: value,std :: unique_ptr< _Ty,std :: default_delete< _Ty>>> :: type std :: make_unique< _Ty,_Types ... >(_ Types&& ... _Args)
function templatestd :: unique_ptr< T,std :: default_delete< T>> make_unique< T,Args ...& ..)
参数类型是:std :: string

第二个应该是一个从make_unique模板调用

  / *将成为c ++ 14的一部分, 
* From:http://herbsutter.com/gotw/_102/
* /
template< typename T,typename ... Args>
std :: unique_ptr< T> make_unique(Args& ... args){
return std :: unique_ptr< T>(new T(std :: forward< Args>(args)...)
}

要转发到的构造函数:

  Shader(const std :: string& name); 

产生错误的代码

  std :: string _name =Shader; 
std :: unique_ptr< Shader> s = make_unique< Shader>(_ name);


解决方案

调用不明确, / em>有 std :: make_unique ,如您所引用的工具提示内容所示。即使你没有写 std :: ,因为你传递一个 std :: string 依赖于参数的查找将自动搜索该命名空间



当你说我使用C ++ 11时,这不太对,因为Visual Studio不允许你选择要写的标准。它只是为你提供了最新的支持任何给定功能。显然,Visual Studio 2013有C ++ 14的 std :: make_unique



删除你的。 P>

Could someone please explain how to resolve the ambigious overload warning for make_unique, where the error comes from and what it does exactly mean (I do understand what an ambigious overload is but I am unsure why I get one for this particular code)? I am using c++11, therefore I use the recommended template from Herb Sutter.

Using it I get the following error:

Error   4   error C2668: 'make_unique' : ambiguous call to overloaded function

And the hover over tooltip in visual studio 13 gives me the following to methods:

function template "std::enable_if<!std::is_array<_Ty>::value, std::unique_ptr<_Ty,std::default_delete<_Ty>>>::type std::make_unique<_Ty,_Types...>(_Types &&..._Args)"
function template "std::unique_ptr<T, std::default_delete<T>> make_unique<T,Args...>(Args...)
argument types are: std::string

The second one should be the one called from the make_unique template

/* Will be part of c++14 and is just an oversight in c++11
 * From: http://herbsutter.com/gotw/_102/
 */
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique(Args&& ...args){
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

The constructor to be forwarded to:

Shader(const std::string& name);

The code generating the error

std::string _name = "Shader";
std::unique_ptr<Shader> s = make_unique<Shader>(_name); 

解决方案

The call is ambiguous because you do have std::make_unique, as shown by the tooltip contents you quote. And even though you did not write std::, since you are passing a std::string argument-dependent lookup kicks in to search that namespace automatically.

When you say "I am using C++11", that's not quite right, because Visual Studio doesn't let you choose which standard to write in. It just provides you with the latest support it has mustered for any given feature. And, apparently, Visual Studio 2013 has C++14's std::make_unique.

Remove yours.

这篇关于c ++ 11/14 make_unique ambigious overload for std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆