Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11268

Re: Odd error I can't seem to recreate

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Odd error I can't seem to recreate
Date 2011-11-04 16:43 -0400
Organization A noiseless patient Spider
Message-ID <j91iqa$urv$1@dont-email.me> (permalink)
References <bbe45080-3e8d-4e2f-aefc-7ac0906a6872@s7g2000yqa.googlegroups.com> <op.v4ft0tyaon14i0@gp.homenet.telecomitalia.it> <185163d7-5063-4ed1-958d-592b377b3b31@k10g2000yqn.googlegroups.com>

Show all headers | View raw


On 11/4/2011 4:07 PM, Noah Roberts wrote:
> [.. seems that auto_ptr is to blame ..]
> I recreated the problem:
>
> cat wtf.cpp
> #include<memory>
>
> struct base
> {
>          virtual ~base() {}
> };
>
> struct derived : base
> {
>    std::auto_ptr<int>  i;
>    derived() : i() {}
> //private:
>    //derived(derived const&);
> };
>
> struct base;
> struct base2
> {
>          virtual ~base2() {}
>          virtual void fun(base const&) const = 0;
> };
> struct derived2 : base2
> {
>          void fun(base const&) const {}
> };
>
> 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());
> }

Comeau online compiles it without even a blink.  So does VC++ 2010 on my 
machine.

> Now I get all the errors I don't expect that are giving me crap in the
> original code:
>
> g++ wtf.cpp
> wtf.cpp: In function âint main()â:
> wtf.cpp:42: error: no matching function for call to
> âderived::derived(derived)â
> wtf.cpp:9: note: candidates are: derived::derived(derived&)
>
> Victor said:
> So, 'test_service' copy constructor takes a non-const reference.  To
> initialize the argument to your 'run_query', a temporary of type
> 'location_service' has to be created (or has to be able to be
> created),
> and I am guessing there is no conversion that the compiler can use.
> .................
>
> As I understood it, because it takes a non-const reference the
> temporary created by the "test_service()" call should be directly
> used, rather than a copy of it attempted.  Why is it necessary to make
> a temporary of 'location_service', or 'base' in the above code?

I may just as well be wrong here.

So far I failed to reproduce your issue with another compiler.  I don't 
have access to gcc/g++ at this time.

Let's drop the auto_ptr member and try declaring our own copy-c-tor that 
takes a non-const ref...

I've dropped the 'i' member from 'derived' and instead added a copy 
c-tor with a non-const ref argument, both Comeau online and VC++ still 
compiled it.

Something is funky with g++, I am guessing.  Can it compile this:

    struct base
    {
       virtual ~base() {}
    };

    struct derived : base
    {
       derived() : base() {}
       derived(derived& d) : base(d) {}
    };

    void fun(base const& b) { return; }

    int main()
    {
       derived d;
       fun(d);
       fun(derived());
    }

?

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 11:52 -0700
  Re: Odd error I can't seem to recreate Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-04 15:49 -0400
    Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 12:58 -0700
      Re: Odd error I can't seem to recreate Richard Damon <news.x.richarddamon@xoxy.net> - 2011-11-04 16:15 -0400
  Re: Odd error I can't seem to recreate "Gennaro Prota" <gennaro.prota@gmail.com> - 2011-11-04 21:03 +0100
    Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 13:07 -0700
      Re: Odd error I can't seem to recreate Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-04 16:43 -0400
        Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 13:56 -0700
          Re: Odd error I can't seem to recreate "Gennaro Prota" <name.surname@gmail.com> - 2011-11-04 22:16 +0100
    Re: Odd error I can't seem to recreate "Gennaro Prota" <gennaro.prota@gmail.com> - 2011-11-04 21:16 +0100
  Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 13:15 -0700
    Re: Odd error I can't seem to recreate Ian Collins <ian-news@hotmail.com> - 2011-11-05 12:00 +1300
  Re: Odd error I can't seem to recreate "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-05 18:48 +0100
    Re: Odd error I can't seem to recreate "Gennaro Prota" <name.surname@gmail.com> - 2011-11-05 22:35 +0100

csiph-web