Re: template copy constructor

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Vladimir Grigoriev <vlad.moscow@xxxxxxx> wrote:
Well, I have started from the very beginning. I have written the
auto_ptr without template functions, and it works.

That's because you don't have any constructors from "wrong" odd_ptr
type.

For constructions as the following

odd_ptr<int> pi1( new int( 10 ) );

odd_ptr<int>pi2 = pi1;

also the copy constructor is called, i.e. the compiler skips the step
of generating odd_ptr<int>( pi1 ).

odd_ptr<int>pi2 = pi1;

is equivalent to

odd_ptr<int>pi2(pi1);

because both are of the same type. From C++98 standard:

8.5p14
- If the initialization is direct-initialization, or if it is
copy-initialization where the cv-unqualified version of the source type
is the same class as, or a derived class of, the class of the
destination... [long description of direct initialization snipped]
- Otherwise (i.e., for the remaining copy-initialization cases)... [long
description of copy initialization snipped]

So, you need constructors involving unrelated types in order to
reproduce the issue.

However looking through some articles about auto_ptr I do not find
sometimes such operator as

odd_ptr & operator=( odd_ptr_ref<T> rhs ) throw()
{
reset( rhs.r.release() );
return ( *this );
}

There should be one. See DR127:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#127

Is set of constructors and operators for auto_ptr predefined in C++
standard?

Yes. From C++98 20.4.5:

namespace std {
template<class X> class auto_ptr {
template <class Y> struct auto_ptr_ref {};
public:
typedef X element_type;

// 20.4.5.1 construct/copy/destroy:
explicit auto_ptr(X* p =0) throw();
auto_ptr(auto_ptr&) throw();
template<class Y> auto_ptr(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr&) throw();
template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
~auto_ptr() throw();

// 20.4.5.2 members:
X& operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p =0) throw();

// 20.4.5.3 conversions:
auto_ptr(auto_ptr_ref<X>) throw();
template<class Y> operator auto_ptr_ref<Y>() throw();
template<class Y> operator auto_ptr<Y>() throw();
};
}

From the draft C++0x D.9:

namespace std {
template <class Y> struct auto_ptr_ref { };

template <class X> class auto_ptr {
public:
typedef X element_type;

// D.9.1.1 construct/copy/destroy:
explicit auto_ptr(X* p =0) throw();
auto_ptr(auto_ptr&) throw();
template<class Y> auto_ptr(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr&) throw();
template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr_ref<X> r) throw();
~auto_ptr() throw();

// D.9.1.2 members:
X& operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p =0) throw();

// D.9.1.3 conversions:
auto_ptr(auto_ptr_ref<X>) throw();
template<class Y> operator auto_ptr_ref<Y>() throw();
template<class Y> operator auto_ptr<Y>() throw();
};

template <> class auto_ptr<void>
{
public:
typedef void element_type;
};
}

The differences are mostly in response to DR127.

Does the above assignment operator exist in the standard
set of functions for auto_ptr

Not in C++98, but that's considered a defect. Yes in C++0x.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Benefits of Protected Construtors in MFC
    ... I want to ask you about logic behind the protected constructors in MFC. ... virtual void Dumpconst; ... CMultiDocTemplate * GreenTemplate; ... cannot be used already in either a string, icon, or menu context. ...
    (microsoft.public.vc.mfc)
  • Re: Benefits of Protected Construtors in MFC
    ... I use public constructors when I have embedded FormViews derived from a class which lives in a static library. ... > virtual void Dumpconst; ... CMultiDocTemplate * GreenTemplate; ... cannot be used already in either a string, icon, or menu context. ...
    (microsoft.public.vc.mfc)
  • Re: [PHP] How can I do this -- method chaining
    ... I believe constructors return void, ... the 'new' keyword returns a copy of the object. ... public static function getInstance() { ...
    (php.general)
  • Re: Best way to develop in parallel
    ... Both have exactly the same method signatures, constructors, etc. ... How can I easily switch between the classes? ... Have the test code refer to the interface and construct one or the other based on some condition. ... void testB() ...
    (comp.lang.java.help)
  • Re: Simple class with self-referencing member fail to run: StackOverflowException
    ... "pedestrian via DotNetMonster.com" wrote in message ... What I mean should be readonly instead of const: ... By making the FireYear and GoodStone objects static, there is no creation of them each call to either of the constructors. ... This is why the stack overflow does not happen, as you have eliminated the recursion issue. ...
    (microsoft.public.dotnet.general)