Prev: Is there any standard/guarantees for exception safety in STL operations?
Next: problem with partial ordering of classes
From: Seungbeom Kim on 28 Jul 2010 05:21 On 2010-07-27 07:45, Daniel Kr�gler wrote: > On 27 Jul., 07:31, Dragan Milenkovic <dra...(a)plusplus.rs> wrote: >> >> And about treating Foo<Derived> as Foo<Base>; it can >> somewhat be solved by a conversion operator, right? > > Typical implementations do this by providing a converting > constructor, yes. This is the way unique_ptr and shared_ptr > are specified. This does not allow for binding a *pointer* > or reference of type unique_ptr<Derived> to a corresponding > object of type unique_ptr<Base>, though. Another disadvantage is that, as far as I know, conversion from unique_ptr<Derived> to unique_ptr<Base> is not considered covariant. // okay Base* Base::clone() const; Derived* Derived::clone() const; // error unique_ptr<Base> Base::clone() const; unique_ptr<Derived> Derived::clone() const; -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thiago A. on 28 Jul 2010 10:27
> > * Better error messages > > This is in my view the best and only argument for unique type modifier. One more point: Sometimes we use pointers only to hold polymorphic items. For instance, at the beginning the code was like this: DoSomething(const FilterBase& ) {...} { const SpecialFilter specialFilter; DoSomething(specialFilter); } ....and then it had to be changed to select the Filter in runtime: { std::auto_ptr<const FilterBase> pFilter = SelectFilterClass(arguments); DoSomething(*pFilter.get()); } In this case the pointer is not what I want because I don�t need the address concept or pFilter++ or iterator concept and it cannot be null. All I want is a polymorphic object in this scope. The point is: Is the unique modifier applicable for references as well? What is the "smart reference class"? If we had this, the code would be expressed as: { const unique FilterBase & filter = SelectFilterClass(arguments); } Different syntaxes could be: auto FilterBase & filter = SelectFilterClass(arguments); delete FilterBase & filter = SelectFilterClass(arguments); and if could express the idea of "list of polimorphic items non null" vector<unique FilterBase& > v; Everthing is about RAII and the language. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |