|
|
Home » U++ Library support » U++ Core » why not "T & Add(const T & x)" in all containers
|
|
|
|
|
|
|
|
|
Re: why not "T & Add(const T & x)" in all containers [message #27844 is a reply to message #27700] |
Fri, 06 August 2010 11:27   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
kohait00 wrote on Thu, 29 July 2010 16:17 | just a short question:
i noticed that all containers dont return a ref on the item created inside a container, when a copy Add is used: (here Array is example)
T& Add();
void Add(const T& x); //why void?
void AddPick(pick_ T& x); //why void?
T& Add(T *newt);
|
Mostly because of standard usage pattern...
It might be a little bit confusing as those variants that are taking parameter make a copy of this parameter (and would return a reference to this copy).
Also note the existence of Top() - only one more line...
But I am not strongly opposed to changing this either....
[Updated on: Fri, 06 August 2010 11:30] Report message to a moderator
|
|
|
|
|
|
Re: why not "T & Add(const T & x)" in all containers [message #28101 is a reply to message #27998] |
Mon, 16 August 2010 09:06   |
 |
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
it's actually the same behaviour as with T& Array::Attach(T* newt);
and merly a logical unification of interface, that anything that ends beeing an object, no matter newly added or as copy, should be instantly available, without the need to again access the container to get the same. here, it actually doesnt matter the container type, it's same situation for Vector and Array.
a practical use is this:
crating new container objects, based on some 'template' objects, and remodifying stuff that is actually different, on the new created object, pushing it somewhere to do something. this would use in case of Array:
void Array::Add(cibst T&),
then
T& Array::operator[](int i) with Array::GetCount()-1.
actually 3 invokations, that could be done in one.
it's maybe more of estetic use but could again add to Ultimate's short and reading friendly code
|
|
|
|
|
|
|
Re: why not "T & Add(const T & x)" in all containers [message #28605 is a reply to message #28588] |
Wed, 08 September 2010 10:24   |
rylek
Messages: 79 Registered: November 2005
|
Member |
|
|
Hi there!
It is a pleasure to play the third party, though the semantics of the term are highly disputable here. I think it's possible Mirek originally didn't include the return reference in Vector::Add to emphasize the fact that such references into Vectors are volatile in principle (specifically they are periodically invalidated by the Add function itself while reallocating the physical Vector data). But, of course, the same argument holds for Add() which does return the reference. Moreover, Array::Add(T *newobj) also returns the reference, albeit for different reasons.
U++ also decidedly avoids returning references in pick assignment operators, which is natural because a "chain" assignment (a = b = c) in such cases exhibits undesirable behaviour (by destroying b). But Vector::Add(const T&) doesn't have this problem; the only thing that has to be avoided is rather artificial constructs of the form
vector.Add(vector.Add(obj))
exactly because of the periodical Vector reference invalidation. But then again you can run into exactly the same problems by writing, e.g.
so that this is no specific of Vector::Add(const T&) either. To sum it all up, I currently see no practical reasons against modifying Vector::Add to include the return reference. I would rather say that it's like updating old code to match interface standards adopted / developed later on, in fact I believe Vector is one of the very oldest things in U++ (although it's been rewritten quite a few times since its inception).
Regards
Tomas
|
|
|
|
Goto Forum:
Current Time: Tue Apr 29 09:48:17 CEST 2025
Total time taken to generate the page: 0.01401 seconds
|
|
|