有人可以解释一下如何解决 make_unique 的模糊重载警告、错误来自何处以及它的确切含义吗(我确实理解什么是模糊重载,但我不确定为什么我会为这个特定代码得到一个)?我使用的是 c++11,因此我使用 Herb Sutter 推荐的模板。
使用它我收到以下错误:
Error 4 error C2668: 'make_unique' : ambiguous call to overloaded function
将鼠标悬停在 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
第二个应该是从 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)...));
}
要转发到的构造函数:
Shader(const std::string& name);
生成错误的代码
std::string _name = "Shader";
std::unique_ptr<Shader> s = make_unique<Shader>(_name);