Search

Argument dependent lookup chooses the wrong function inside sizeof statements by Fred Sundvik

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 634966
Opened: 1/6/2011 3:00:19 PM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
This applies to c++

Inside a sizeof statemen, when there's a template mebmer function, and another function outside the class with the same name, the compiler sometimes wrongly chooses the outside one.

More specifcally, it always chooses the function from a namespace matching it's argument, even if the member function is explicitely specified as shown in the example code. If the outside function doesn't have the correct signature for the parameter, then a compiler error is generated.
Details (expand)

Visual Studio/Silverlight/Tooling version

Visual Studio 2010

What category (if any) best represents this feedback?

Compatibility

Steps to reproduce

Compile the follwing c++ code
#include <iostream>

class A
{
};

void SameName(void* value)
{
}

void SameName2(void* value)
{
}

char SameName3(A value)
{
    return 'a';
}



namespace Test
{
    class A
    {
    };
}

namespace Test2
{
    void SameName(void* value)
    {
    }

    class A
    {
    };
}

class Bug
{
public:
    template<typename T2>
    static int SameName(T2 value)
    {
        return 1;
    }

    static int SameName2(A value)
    {
        return 1;
    }

    template<typename T2>
    static int SameName3(T2 value)
    {
        return 1;
    }

    template<typename T>
    static int Func()
    {
    }

    //Works
    static const int value1=sizeof(Bug::SameName(1));
    // Doesn't work, both A and SameName functions are in the
    // global namespace
    static const int value2=sizeof(Bug::SameName(A()));
    // Works there's no SameName function inside the Test namespace
    static const int value3=sizeof(Bug::SameName(Test::A()));
    // Doesn't work, there's a SameName function inside the Test2
    // namespace
    static const int value4=sizeof(Bug::SameName(Test2::A()));
    // Works, non template functions doesn't bug the same way
    static const int value5=sizeof(Bug::SameName2(A()));
    static const int value6=sizeof(Bug::SameName3(A()));

    // The exact same code works when put inside a function
    static int TestFunction()
    {
        const int value1=sizeof(Bug::SameName(1));
        const int value2=sizeof(Bug::SameName(A()));
        const int value3=sizeof(Bug::SameName(Test::A()));
        const int value4=sizeof(Bug::SameName(Test2::A()));
        const int value5=sizeof(Bug::SameName2(A()));
        const int value6=sizeof(Bug::SameName3(A()));
        return value6;
    }

};


int main()
{
    if(Bug::TestFunction()==Bug::value6)
    {
        std::cout << "Correct function" << std::endl;
    }
    else
    {
        std::cout << "Wrong function" << std::endl;
    }

    return 0;
}


Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

The following compiler errors are produced
compilerbug.cpp(69): error C2664: 'SameName' : cannot convert parameter 1 from 'A' to 'void *'
         No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
compilerbug.cpp(69): error C2866: 'Bug::value2' : a const static data member of a managed type must be initialized at the point of declaration
compilerbug.cpp(74): error C2664: 'Test2::SameName' : cannot convert parameter 1 from 'Test2::A' to 'void *'
         No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

If the offending lines are commented out. The program runs but prints "Wrong function" to the console.

Expected results

No errors, with the output "Correct function" printed to the console. Note the code should find the correct function, even without explicitely specifying that the function should be found inside the Bug class.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Fred Sundvik on 1/19/2011 at 2:02 PM
In which version of visual studio will this be fixed?

Just checking that the problem is actually fixed, instead of just being automatically closed because of no activity, or not being able to repeat.
Posted by Microsoft on 1/6/2011 at 9:15 PM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 1/6/2011 at 3:14 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.