Bug 24011 - ambiguous overload reported for no obvious reason
Summary: ambiguous overload reported for no obvious reason
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-09-22 15:08 UTC by Andrew Dorrell
Modified: 2005-09-22 15:17 UTC (History)
1 user (show)

See Also:
Host: redhat.x86.linux
Target: redhat.x86.linux
Build: redhat.x86.linux
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Dorrell 2005-09-22 15:08:46 UTC
// Barebones code to reproduce the problem: 
// Interface 
namespace NS 
{ 
    template<typename T> class X {}; 
 
    template<typename T> X<T> operator*(const X<T> &a, const X<T> &b); 
} 
 
// Implementation 
template<typename T> 
NS::X<T>operator*(const NS::X<T> &a,const NS::X<T> &b) 
{ 
    return NS::X<T>(); 
} 
 
// Application 
int main(int argc, char *argv[]) 
{ 
    NS::X<int> tmp = NS::X<int>() * NS::X<int>(); 
} 
 
 
 
What I get: 
 
-bash-3.00$ uname -a 
Linux elisha.research.canon.com.au 2.6.12-1.1447_FC4smp #1 SMP Fri Aug 26 
20:57:13 EDT 2005 i686 i686 i386 GNU/Linux 
-bash-3.00$ gcc --version 
gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5) 
Copyright (C) 2005 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions.  There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 
 
-bash-3.00$ make strange 
g++ -g    strange.cpp   -o strange 
strange.cpp: In function 'int main(int, char**)': 
strange.cpp:19: error: ambiguous overload for 'operator*' in 'NS::X<int>() * 
NS::X<int>()' 
strange.cpp:11: note: candidates are: NS::X<T> operator*(const NS::X<T>&, 
constNS::X<T>&) [with T = int] 
strange.cpp:6: note:                 NS::X<T> NS::operator*(const NS::X<T>&, 
const NS::X<T>&) [with T = int] 
make: *** [strange] Error 1 
 
 
If I place the implementation within the scope of the namespace geometry{...} 
then it seems to compile fine.  However the above does not appear to cause 
problems using MSVC.  Problem exists with gcc 3.3.2 and 3.3.4.  Is this code 
incorrect??? 
 
Thanks
Comment 1 Andrew Pinski 2005-09-22 15:17:27 UTC
the error message about ambiguous overload is correct as there are two functions there.
operator* in the global namespace and operator* in the NS namespace.
You most likely wanted to implement operator* in the NS namespace and not a new one in the global 
namespace.

The following code does what you wanted to do:
// Barebones code to reproduce the problem: 
// Interface 
namespace NS
{
    template<typename T> class X {};

    template<typename T> X<T> operator*(const X<T> &a, const X<T> &b);
}

// Implementation 
template<typename T>
NS::X<T>NS::operator*(const NS::X<T> &a,const NS::X<T> &b)
{
    return NS::X<T>();
}

// Application 
int main(int argc, char *argv[])
{
    NS::X<int> tmp = NS::X<int>() * NS::X<int>();
}

Notice how I wrote the Implementation.
Comment 2 Andrew Dorrell 2005-09-23 13:34:55 UTC
Subject: Re:  ambiguous overload reported for no obvious reason

Thanks - obvious when its pointed out.  Sorry for the bother.

On Friday 23 September 2005 01:17, pinskia at gcc dot gnu dot org wrote:
> The following code does what you wanted to do:
...
> Notice how I wrote the Implementation.