C ++ 11/14 make_unique ambiguous overload for std :: string

Can someone explain how to resolve the ambiguous overload warning for make_unique, where the error occurs and what exactly it means (I understand what is ambiguous overload, but I'm not sure why I get one for this specific code)? I use C ++ 11, so 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 hovering over the tooltip in visual studio 13 gives me the following 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 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 redirected to:

 Shader(const std::string& name); 

Error code

 std::string _name = "Shader"; std::unique_ptr<Shader> s = make_unique<Shader>(_name); 
+6
source share
2 answers

The call is ambiguous because you have std::make_unique , as shown in the tip you provided. And even if you do not write std:: , because you pass the std::string argument-dependent search to automatically search for this namespace .

When you say "I am using C ++ 11", this is not entirely correct, because Visual Studio does not allow you to choose which standard to write. It simply provides you with the latest support that it has gathered for any given feature. And apparently, Visual Studio 2013 has C ++ 14 std::make_unique .

Remove yours.

+3
source

It would seem that it is impossible to do in a visual studio.

This is mistake.

Best of all, according to this question, How to detect if I compile code using Visual Studio 2008? continue to use various #ifdefs for where the visual studio is still too trashy to support basic functions.

Using:

 #if (_MSC_VER == 1500) // ... Do VC9/Visual Studio 2008 specific stuff #elif (_MSC_VER == 1600) // ... Do VC10/Visual Studio 2010 specific stuff #elif (_MSC_VER == 1700) // ... Do VC11/Visual Studio 2012 specific stuff #endif 
-1
source

Source: https://habr.com/ru/post/982506/


All Articles