This code does not generate an error or warning: class A{}; class B : public A {}; template <class T> class SmartPtr{ public: template<typename OtherT> operator const SmartPtr<OtherT>&() const; template<typename OtherT> operator SmartPtr<OtherT>() const; }; void func(SmartPtr<A>) { } int main() { SmartPtr<B> b; func(b); // this should be ambiguous? } But it is an error with MSVC 10 and Clang 2.8. If the templates on the operators are removed, it also becomes ambiguous with gcc: class A {}; class B : public A {}; template <class T> class SmartPtr { public: operator const SmartPtr<A>&() const; operator SmartPtr<A>() const; }; void func(SmartPtr<A>) { } int main() { SmartPtr<B> b; func(b); // func is ambiguous }
I have never worked with the GCC source code before, any pointers to where I should start to look to fix this issue? Thanks!
(In reply to comment #1) > I have never worked with the GCC source code before, any pointers to where I > should start to look to fix this issue? Thanks! http://gcc.gnu.org/wiki/DebuggingGCC You could put a breakpoint for the second testcase in error(). Find out why the error triggers, and then find out what is the difference with the first testcase. In any case, the relevant code is in gcc/cp/. I hope you don't get scared by the looks. It ain't pretty. However, this seems to be fixed in trunk (at least revision 180166): manuel@gcc12:~$ ~/trunk/180166/build/gcc/cc1plus pr50306.cc void func(SmartPtr<A>) int main() pr50306.cc:15:9: error: conversion from ‘SmartPtr<B>’ to ‘SmartPtr<A>’ is ambiguous pr50306.cc:14:15: note: candidates are: pr50306.cc:7:3: note: SmartPtr<T>::operator SmartPtr<A>() const [with T = B] pr50306.cc:6:3: note: SmartPtr<T>::operator const SmartPtr<A>&() const [with T = B] pr50306.cc:10:6: error: initializing argument 1 of ‘void func(SmartPtr<A>)’ Maybe you should try 4.6.2?
I tried 4.6.2, r181503 and r180166 and they all compiled the sample code without error. Do you have any local changes or special parameters that made it not compile?
Yes, I confirm the issue doesn't seem fixed neither mainline nor 4_6-branch.
You are right, it seems I tested the second example by mistake.