std::auto_ptr

  • Follow


A short control question. Isnt the following valid C++

#include <memory>

class A {};
class B : public A {};

void foo(std::auto_ptr<A> a) {}

int main(int argc, char* argv[])
{
 std::auto_ptr<B> b(new B());
 foo(b); // Fails with C2664 in VS2005 (succeds in VC7.1)
 foo(std::auto_ptr<B>(b)); // succeds
 return 0;
}

The code marked fails in VC7.1 but fails in VC8 (VS2005).

Regards
/Michel

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply michel.andre (3) 12/11/2006 11:10:32 PM

Michel Andr� wrote:
> A short control question. Isnt the following valid C++
>
> #include <memory>
>
> class A {};
> class B : public A {};
>
> void foo(std::auto_ptr<A> a) {}
>
> int main(int argc, char* argv[])
> {
>  std::auto_ptr<B> b(new B());
>  foo(b); // Fails with C2664 in VS2005 (succeds in VC7.1)
>  foo(std::auto_ptr<B>(b)); // succeds
>  return 0;
> }
>
> The code marked fails in VC7.1 but fails in VC8 (VS2005).
>
> Regards
> /Michel
AFAIK std::auto_ptr<B> does not inherit from std::auto_ptr<A> thus it
is a completely different class and can't be passed to the function.
Try making foo a template function:

template <typename T>
void foo(std::auto_ptr<T> t) {}

----
Ivan
http://www.0x4849.net


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Ivan 12/12/2006 8:27:22 AM


Michel Andr� wrote:

> A short control question. Isnt the following valid C++
>
> #include <memory>
>
> class A {};
> class B : public A {};
>
> void foo(std::auto_ptr<A> a) {}
>
> int main(int argc, char* argv[])
> {
>  std::auto_ptr<B> b(new B());
>  foo(b); // Fails with C2664 in VS2005 (succeds in VC7.1)
>  foo(std::auto_ptr<B>(b)); // succeds
>  return 0;
> }

It's a known std::auto_ptr problem. Please see
http://groups.google.com/group/comp.lang.c++.moderated/msg/a60efdd76f694a00


--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Maxim 12/12/2006 10:20:56 AM

"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> writes:

> It's a known std::auto_ptr problem. Please see
>
http://groups.google.com/group/comp.lang.c++.moderated/msg/a60efdd76f694a00

http://groups-beta.google.com/group/comp.std.c++/browse_thread/thread/bcba25
fcf7682e7d
discusses how to fix the problem.

Also see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1852.html#463
I think the title of this issue understates the problem, though.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply David 12/12/2006 5:02:15 PM

3 Replies
153 Views

(page loaded in 0.063 seconds)

Similiar Articles:






7/15/2012 4:09:12 PM


Reply: