Re: Overload resolution and copy constructors that take non-const
references
On May 7, 11:44 am, Anthony Williams <anthony_w....@yahoo.com> wrote:
Hi,
Should the following compile, and what should it print?
Really, I don't know.Sorry to answer question
with question.
#include <memory>
#include <iostream>
void foo(std::auto_ptr<int> x)
Is same as
void foo(const std::auto_ptr<int> x) ?
struct dummy
{
dummy(std::auto_ptr<int> const&)
{}
};
void foo(dummy x)
....
int main()
{
std::auto_ptr<int> const x(new int);
foo(x);
}
MSVC 9.0 and g++ 4.3 disagree.
MSVC compiles it and prints "dummy const ref", which is what I expected.
g++ refuses to compile it, saying that:
....
It isn;t clear to me following example:
class A{
public:
A(){}
A(A&){}
};
A foo(){ return A(); }
int main()
{
A a;
a = foo();
}
g++ gives strange error:
cctor.cpp: In function =91A foo()':
cctor.cpp:7: error: no matching function for call to =91A::A(A)'
cctor.cpp:4: note: candidates are: A::A(A&)
cctor.cpp: In function =91int main()':
cctor.cpp:12: error: no matching function for call to =91A::A(A)'
cctor.cpp:4: note: candidates are: A::A(A&)
I don;t have msvc handy but I think saw somewhere,
that, it compiles without error.
So, perhaps, as g++ wants A(A) this is similar to
your question.
As I never used non const reference copy constructor,
I'm really confused about it's behavior.
It seems to me that compiler writers are too ;)
Greetings, Branimir.