Visual Studio warning about function not in global namespace

LostMikely

I didn't really know what to write in the title, but basically I have a single .cpp, with only standard library headers included and no "using" keywords. I made my own "generate(...)" function. After including the library, Visual Studio shows me an error (where the function is being called), basically saying that it doesn't know whether to choose std::generate(...) or generate(...) because they have matching argument lists.

Is this a bug or have I missed something? I might 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 ==========
ThePhD

This happens on not just VC++ (VS 2015), but g++ 4.9+ as well. The issue here is the tricky Argument Dependent Lookup (Koenig Lookup).

It looks at the two iterators you're adding and it sees the "generate" function in std because the iterators also come from the std namespace (this is the point of Argument Dependent Lookup).

This problem actually bit me at one point: when I wrote my own tie implementation that did a few things extra to tie. I had to call mine tye because Koenig Lookup caused the considered overloads to be equal in their ranking and thus cause an error like this.

Either prefix generate with :: to start lookup from the global namespace (::generate( vec.begin(), vec.end(), ... );), or name it differently.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to let TypeScript know about a function attached the the global namespace

From Dev

Visual Studio warning about copies of files with different contents

From Dev

Why is a sort function in the global namespace?

From Dev

Odd warning about iconv() function

From Dev

Error and Warning in visual studio 2010

From Dev

Function definition not found for a function declared inside unnamed namespace - how to resolve? (Visual Studio 2015)

From Dev

Pycharm visual warning about unresolved attribute reference

From Dev

Global namespace scope operator on function definition

From Dev

Copy module function declarations to global namespace

From Dev

friend function in global namespace with custom return type

From Dev

friend function in global namespace with custom return type

From Dev

global namespace function not available in namespaced class constructor

From Dev

warning: inline qualifier ignored for "global" function

From Dev

Implicit cast on function return in C and why Visual studio doesn't throw warning

From Dev

Visual Studio is not reading my dll? (as a namespace)

From Dev

Visual Studio 2013 incorrect namespace errors

From Dev

Hide namespace in Quick Info from Visual Studio

From Dev

Visual studio Test Explorer Group by name Namespace

From Dev

How to make Visual Studio forget a namespace?

From Dev

XAML: namespace strange behaviour Visual Studio

From Dev

Do a Find in Current Namespace in Visual Studio?

From Dev

Is there a flat namespace representation option for Visual Studio

From Dev

Visual studio does not exist in the namespace, but reference is added

From Dev

Enable Mismatch namespace warnings in Visual Studio 2010

From Dev

Namespace Issues in Visual Studio Automated Builds

From Dev

Adding Namespace Artifacts to Visual Studio Layering Diagram

From Dev

Visual studio: Can and cannot access a namespace

From Dev

name must be a namespace name Visual Studio Error

From Dev

Visual Studio 2017 and WinJS - Autocomplete for "Windows" namespace

Related Related

  1. 1

    How to let TypeScript know about a function attached the the global namespace

  2. 2

    Visual Studio warning about copies of files with different contents

  3. 3

    Why is a sort function in the global namespace?

  4. 4

    Odd warning about iconv() function

  5. 5

    Error and Warning in visual studio 2010

  6. 6

    Function definition not found for a function declared inside unnamed namespace - how to resolve? (Visual Studio 2015)

  7. 7

    Pycharm visual warning about unresolved attribute reference

  8. 8

    Global namespace scope operator on function definition

  9. 9

    Copy module function declarations to global namespace

  10. 10

    friend function in global namespace with custom return type

  11. 11

    friend function in global namespace with custom return type

  12. 12

    global namespace function not available in namespaced class constructor

  13. 13

    warning: inline qualifier ignored for "global" function

  14. 14

    Implicit cast on function return in C and why Visual studio doesn't throw warning

  15. 15

    Visual Studio is not reading my dll? (as a namespace)

  16. 16

    Visual Studio 2013 incorrect namespace errors

  17. 17

    Hide namespace in Quick Info from Visual Studio

  18. 18

    Visual studio Test Explorer Group by name Namespace

  19. 19

    How to make Visual Studio forget a namespace?

  20. 20

    XAML: namespace strange behaviour Visual Studio

  21. 21

    Do a Find in Current Namespace in Visual Studio?

  22. 22

    Is there a flat namespace representation option for Visual Studio

  23. 23

    Visual studio does not exist in the namespace, but reference is added

  24. 24

    Enable Mismatch namespace warnings in Visual Studio 2010

  25. 25

    Namespace Issues in Visual Studio Automated Builds

  26. 26

    Adding Namespace Artifacts to Visual Studio Layering Diagram

  27. 27

    Visual studio: Can and cannot access a namespace

  28. 28

    name must be a namespace name Visual Studio Error

  29. 29

    Visual Studio 2017 and WinJS - Autocomplete for "Windows" namespace

HotTag

Archive