I have a class A
template<typename T>
class A : public std::auto_ptr<T>
{
typedef std::auto_ptr<T> Super;
public:
A() : Super() { }
A(T* t) : Super(t) { }
A(A<T>& o) : Super(o) { }
...
};
and IConstEnumerator
template<typename T>
struct IConstEnumerator
{
...
virtual IEnumerator<T>* GetEnumerator() = 0;
virtual IConstEnumerator<T>* GetEnumerator() const = 0;
};
When I run this code
AP< IConstEnumerator<IPort> > pe = node.GetPorts().GetEnumerator(); ;
I got errors from not finding correct match with g++ compiler.
error: no matching function for call to ‘AIR::A<AIR::IConstEnumerator<AIR::IPort> >::AP(AIR::A<AIR::IConstEnumerator<AIR::IPort> >)’
note: candidates are: AIR::A<T>::A(AIR::A<T>&) [with T = AIR::IConstEnumerator<AIR::IPort>]
note: AIR::A<T>::A(T*) [with T = AIR::IConstEnumerator<AIR::IPort>]
What's wrong with the class A? It works well with MSVC.
EDIT
Using copy constructor explicitly seems to solve this issue. A< IConstEnumerator<IPort> > pe(node.GetPorts().GetEnumerator())
Without the exact code that actually causes the error it's hard to know for sure, but I suspect that from
AIR::A<T>::A(AIR::A<T>&)
what happening is you're trying to pass a temporary (possibly implicit) object intoAIR::A<T>::A
and MSVC lets you bind the temporary to the non-const reference parameter, while g++ quite properly prohibits this. Can you make the parameterconst
?Your
A
copy constructor takes its argument by non-const reference:Your example probably tries to copy a temporary object and the non-const reference can't bind to the temporary object. Visual C++ has an evil extension that allows this to work; you have to be careful to avoid relying on that extension if you want your code to be portable.
If you are trying to mimic
auto_ptr
's copy constructor, you need to also implement something similar toauto_ptr_ref
, which is used as a helper to allow copying temporaryauto_ptr
s. I describe how this is accomplished in the accepted answer to How could one implementstd::auto_ptr
's copy constructor?For what it's worth, deriving from
std::auto_ptr
is a bit odd; consider using composition instead of inheritance, if you can (there isn't a whole lot instd::auto_ptr
that you'd benefit from by deriving from it anyway).I'd guess you're lacking
A(A<T> const & o)
.VS (incorrectly) permits getting a non-const reference to a temporary, g++ behaves properly
It looks compiler talks that it cannot choose right function: constructor
A
from pointerA<T>::A(T*)
or copy constructorA<T>::A(A<T> &)
. Maybe it worth to change definition toexplicit A(T* t) : Super(t) { }