My new vector

I've just upgraded my MSVC compiler from 2008 to 2010. Now I come with my new vector - it's much simpler than the previous one. Here's my changes :

- Added functions : push_ptr, push_construct, emplace_back, make_space, copy, is_full, enum_object, unique

- Added custom "construct" functions. You now may choose if you want it to support constructors.

- Faster, simpler. More portable, stable.

Here's my update : https://gist.github.com/kikiman/6094395
Previous : http://www.cplusplus.com/forum/lounge/101858/#msg547618
Last edited on
First thing I noticed: Function definitions which are supplied in the definition of the class are implicitly inline. There is no reason to preface each one with inline.

Other questions:

What's with the union? It appears to be solely for purposes of obfuscation.
What's with everything being public?
Why are you using memcpy as opposed to std::copy?
Why are you using realloc as opposed to new?
Why do you have member functions that don't access member data?
Why are you using assert as opposed to propagating exceptions?
@cire

First thing I noticed: Function definitions which are supplied in the definition of the class are implicitly inline. There is no reason to preface each one with inline


I just want to ensure that all functions must be completely "inline". :)

What's with the union? It appears to be solely for purposes of obfuscation.

Yes they are only like "comments", they are not used so you can remove it.

What's with everything being public?

I want to keep it simple. It's a custom vector. I don't think anyone will call a '_' reserved function for no reason :)

Why are you using memcpy as opposed to std::copy?

I'm worried about performance, so if you want it to support constructors, turn it off. Otherwise if you only want to manage data, turn memcpy on.

Why are you using realloc as opposed to new?

"new" only does allocate. "realloc" is more convenient, it does allocate & copy like "malloc" & "memcpy".

Why do you have member functions that don't access member data?

Yeah, some functions seem like don't access member data. But probably they're used many times, so I packed them. Also I think doing so can give hints to the compiler.

Why are you using assert as opposed to propagating exceptions?

"assert" is not used very much. It only appears on debug and it's used to catch memory exceptions or access (at). I don't think they will appear much so I removed "exceptions" and then replaced with "assert".
Last edited on
Re: Use of memcpy as opposed to std::copy
I'm worried about performance, so if you want it to support constructors, turn it off. Otherwise if you only want to manage data, turn memcpy on.

http://stackoverflow.com/questions/4707012/c-memcpy-vs-stdcopy
Not only can std::copy be more efficient than memcpy, it is also the correct thing to do in a container which is intended to handle arbitrary types.


"new" only does allocate. "realloc" is more convenient, it does allocate & copy like "malloc" & "memcpy".

I would prefer correct to convenient in all situations where I was using a vector intended to handle arbitrary types.


Re: union use
Yes they are only like "comments", they are not used so you can remove it.

You might consider using comments as comments. That's what they're for. Although, self-documenting code is the best. I'd get rid of the one letter identifiers and use the longer ones.
Topic archived. No new replies allowed.