c ++ 11/14 make_unique std :: string的ambigious重载

Kev*_*her 6 c++ templates c++11 c++14

有人可以解释如何解决make_unique的ambigious过载警告,其中错误来自及其确切意味着什么(我确实理解什么是一个ambigious重载但是我不确定为什么我得到一个这个特定的代码)?我使用的是c ++ 11,因此我使用了Herb Sutter推荐的模板.

使用它我收到以下错误:

Error   4   error C2668: 'make_unique' : ambiguous call to overloaded function
Run Code Online (Sandbox Code Playgroud)

并将鼠标悬停在visual studio 13中的工具提示上,为我提供了以下方法:

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
Run Code Online (Sandbox Code Playgroud)

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

/* 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)...));
}
Run Code Online (Sandbox Code Playgroud)

要转发到的构造函数:

Shader(const std::string& name);
Run Code Online (Sandbox Code Playgroud)

产生错误的代码

std::string _name = "Shader";
std::unique_ptr<Shader> s = make_unique<Shader>(_name); 
Run Code Online (Sandbox Code Playgroud)

Lig*_*ica 5

该调用不明确,因为您确实std::make_unique,如您引用的工具提示内容所示。即使您没有编写std::,因为您正在传递一个std::string 依赖于参数的查找,因此会自动搜索该名称空间

当您说“我正在使用 C++11”时,这不太正确,因为 Visual Studio 不允许您选择要编写的标准。它只是为您提供针对任何给定功能的最新支持。而且,显然,Visual Studio 2013 具有 C++14 的std::make_unique.

删除你的。

  • @LightnessRacesinOrbit它是ADL,我认为问题是当你没有全局范围版本时,编译器不知道`make_unique`是*模板名称*,所以它无法解析`&lt;`。http://coliru.stacked-crooked.com/a/33fc5b4228e53f63 (2认同)