@Disch, see the updated file on sourceforge, or the updated reallocate function in an earlier post. I'm aware I haven't pushed the update via git yet, so you may be seeing a slightly dated version. I was also not aware of std::move, I'll look into that. I have done no exception handling yet, I was getting a working class up and running first, then deal with exceptions. My experience with exceptions is limited in C++ so I was going to isolate working with those.
1 2 3 4 5
|
for(int i = 0; i < _size; i++)
{
temp[i] = data[i]; // <- it's very possible that the = operator will throw an exception
// especially if it involves more allocation.
}
|
May I ask why this would throw an exception/cause more allocation?
In regards to the overloaded operator[], I pulled that out of C++ Primer. I don't have it on me right now, but it stated if you're going to overload operator[] you should have both versions that I have included (though I'm not too sure why, the explanation was rather small).
@catfish...
Also what's the use for the else branch in the code below? |
I wanted to make sure it would still work correctly even if size was some strange value for some reason (ie negative). I do have size getting decremented in points, so the possibility is there.
After reading that SO link, it doesn't seem like there's really an issue with underscore first as long as the first letter is lowercase, but I'll go in change it. Don't want anything silly to happen because of that :P
@Framework (big post),
1,2) Yea that hadn't occurred to me when writing this. It's obvious now so I'll definitely get on changing it.
3) I had a question about this earlier, I wasn't sure if I should validate it there or let the hypothetical user of this class deal with validating the indices.
4) Responded to above.
5) Why should it need to be const? And once the allocation is made exception safe, and _capacity is only changed when it's valid, should I need to do any checks here?
6) Yea yea I guess it's a little too much.
1) I was gonna work out who to inline once I was happy with the implementation. I don't like to try to optimize a rough draft :)
2) This one is the most interesting to me, though I don't fully understand what you're saying. Mind clarifying there?
3) I figured 10 was a good enough default value (what does std::vector use?), if the user needs a larger initial capacity they can use the second constructor. I do plan on adding a reserve function as well. As an aside, doubling the size at each reallocation was just kind of a place holder. I'm curious if there is a better mathematical way to go about it?
4) I'll look this over. But how would pointers be any faster than indexing arrays?
5) Also interesting, I hadn't thought of this. I also wasn't aware passing scalar types by reference was slower? How would this be fixed, though? I'm not sure how I would be able to distinguish between scalar and object types.
6) Won't this just affect compilation time?
@helios,
I was kind of feeling the same way. The added work to make this work with objects with no default constructor seems like a lot when the alternative is just say "objects used with this must have default constructor". Though this is all just for practice to brush up before a big internship interview, so I may still go ahead and add the functionality.
I'd prefer to not use malloc(), though I guess I don't have anything against it. Placement new is completely new to me, I haven't even heard of it until now. So I guess I'll need to look that up a bit.
p.s.,
I appreciate all the input here! This is awesome. Also, you should look at the file on sourceforge, not the commit on git as I haven't figured out how to commit updated files to git yet, so it's still the first version of this. The file is updated though.