Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Vector<int>::At does not return a default constructed value
Vector<int>::At does not return a default constructed value [message #44021] Mon, 15 December 2014 14:00 Go to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
I understand why this would happen with built in types, but is there no fix for this?
Re: Vector<int>::At does not return a default constructed value [message #44027 is a reply to message #44021] Tue, 16 December 2014 07:48 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
cbpporter wrote on Mon, 15 December 2014 14:00
I understand why this would happen with built in types, but is there no fix for this?


Here I do not understand what you mean by "fix"?

As it is now, if you need new elements added to Vector<int> to be initialized to some specific value, e.g. zero, you can specify it as second parameter of .At.

Now by fix you can also mean that int() 'constructor' should be used and initialize value to zero, like std::vector does. Here I am not so sure this is such a good thing; but I am open to debate...

Mirek
Re: Vector<int>::At does not return a default constructed value [message #44032 is a reply to message #44027] Tue, 16 December 2014 12:54 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
mirek wrote on Tue, 16 December 2014 08:48


Now by fix you can also mean that int() 'constructor' should be used and initialize value to zero, like std::vector does. Here I am not so sure this is such a good thing; but I am open to debate...

Mirek


Yes, that's what I meant. I know this is a touchy subject maybe with no real best answer and I was wondering what your stance is on this.

I ask for two reasons: one I needed At and forgot about the two parameter version so I ended up writing more complex code than needed to handle this (fixed now with two param version) and I'm also designing my own mini-library where I'm hitting the same problems with the container classes.
Re: Vector<int>::At does not return a default constructed value [message #44033 is a reply to message #44032] Tue, 16 December 2014 15:16 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, the disadvantage of such "involuntary" initialization is fact that in some cases, compiler is not able to optimize out the zero assignment, thus resulting in negligibly lower performance. IMO, same reasoning is true for local variables...

And then, estetically, I really dislike the anomaly that

int x;

and

int x = int();

are different things...

Mirek

Previous Topic: Stack tracing
Next Topic: Ideographic Character Handling
Goto Forum:
  


Current Time: Thu Mar 28 23:18:01 CET 2024

Total time taken to generate the page: 0.01429 seconds