Visual Studio warning of a non-global namespace function

I really did not know what to write in the title, but basically I have one .cpp, which includes only standard library headers and without the "use" of keywords. I created my own function "generate (...)". After turning on the library, Visual Studio shows me an error (where the function is called), basically saying that it does not know whether to select std :: generate (...) or generate (...) because they are matching argument lists.

Is this a mistake or is something missing? I can also add that I am using VS2015.

#include <iostream> #include <ctime> #include <vector> #include <algorithm> template<typename Iter, typename Function> Function generate(Iter begin, Iter end, Function f) { while (begin != end) { *begin = f(); ++begin; } return f; } class Random { public: Random(int low, int high) : mLow(low), mHigh(high) {} int operator()() { return mLow + rand() % (mHigh - mLow + 1); } private: int mLow; int mHigh; }; class Print { void operator()(int t) { std::cout << t << " "; } }; int main() { srand(time(0)); std::vector<int> intVec; intVec.resize(15); Random r(2, 7); generate(intVec.begin(), intVec.end(), r); } 

Error output:

 1>------ Build started: Project: Functor, Configuration: Debug Win32 ------ 1> Main.cpp 1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(44): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data 1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): error C2668: 'generate': ambiguous call to overloaded function 1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(7): note: could be 'Function generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(Iter,Iter,Function)' 1> with 1> [ 1> Function=Random, 1> Iter=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>> 1> ] 1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(1532): note: or 'void std::generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(_FwdIt,_FwdIt,_Fn0)' [found using argument-dependent lookup] 1> with 1> [ 1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, 1> _Fn0=Random 1> ] 1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): note: while trying to match the argument list '(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, Random)' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+5
source share
1 answer

This happens not only with VC ++ (VS 2015), but also with g ++ 4.9+. The problem here is a complex, dependent argument search (Koenig Lookup) .

It looks at the two iterators you add and sees the "generate" function in std , because iterators also come from the std (this is the search point for the dependent arguments).

This problem actually bit me at some point: when I wrote my own tie implementation, which did several things in addition to tie . I had to call my tye , because Koenig Lookup caused a few overloads to be equal in their ranking and, therefore, cause such an error.

Any prefix is ​​generated using :: to start a search from the global namespace ( ::generate( vec.begin(), vec.end(), ... ); ), or name it differently.

+10
source

All Articles