Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument Lookup in C++ [duplicate]

Tags:

c++

gcc

c++14

Running into an issue with gcc with c++14. When compiling the code below I get an error

"call of overloaded ‘make_unique(std::__cxx11::string)’ is ambiguous"

However if I remove the local definition of make_unique I also get an error:

"‘make_unique’ was not declared in this scope"

It seems like it should be impossible to get both of these errors as either the std::make_unique is pulled in due to ADL or it is not. Is this just an issue with gcc or is there something else going on?

For reference subbing make_unique for a non-template std function (such as stoi) gets rid of the "not declared in this scope" error which leads me to believe it is an issue with gcc.

#include <string>
#include <memory>

template <typename T, typename... Args>
inline std::unique_ptr<T> make_unique(Args&&... args)
{
    return std::unique_ptr<T>( new T(std::forward<Args>(args)...) );
}   

struct A
{
    A(std::string a){ }
};  

int main()
{
    auto a = make_unique<A>(std::string());
}   
like image 601
User3050 Avatar asked May 05 '17 18:05

User3050


1 Answers

It is not a bug.

Without your local template definition,

make_unique<A>(std::string());

we don't have template definition of make_unique available and we have so

(make_unique < A) > (std::string());

With your definition, we have template definition and so, we can use regular ADL.

like image 149
Jarod42 Avatar answered Oct 21 '22 18:10

Jarod42