Re: Odd error I can't seem to recreate
On Nov 4, 11:52 am, Noah Roberts <roberts.n...@gmail.com> wrote:
Under what conditions in which the first could work might the second
not when the parameter is const ref? I can't think of any.
Here's a further revision of the problem code:
cat wtf.cpp
#include <memory>
#include <iostream>
struct base
{
virtual ~base() {}
virtual void f() const = 0;
};
struct derived : base
{
#ifdef HUH
private:
derived(derived const&);
#endif
public:
derived() {}
void f() const { std::cout << "derived::f() \n"; }
};
struct base;
struct base2
{
virtual ~base2() {}
virtual void fun(base const&) const = 0;
};
struct derived2 : base2
{
void fun(base const& b) const { b.f(); }
};
struct base3
{
virtual ~base3() {}
virtual std::auto_ptr<base2> get() const = 0;
};
struct derived3 : base3
{
std::auto_ptr<base2> get() const { return
std::auto_ptr<base2>(new derived2); }
};
int main()
{
derived3 d3;
std::auto_ptr<base2> b2 = d3.get();
b2->fun(derived());
}
The result of compilation and run without -DHUH:
$ g++ wtf.cpp
$ ./a.out
derived::f()
The result with -DHUH:
$ g++ -DHUH wtf.cpp
wtf.cpp: In function =E2int main()=E2:
wtf.cpp:14: error: =E2derived::derived(const derived&)=E2 is private
wtf.cpp:47: error: within this context
So it appears that the problem is with the temporary being created,
and that I don't understand. As far as I knew, const reference
allowed you to use the initially created temporary directly. Where's
the standard text that explains why it's doing this?