patterncppMinor
C-style array class
Viewed 0 times
arraystyleclass
Problem
I often use C API's in C++ that force me to use C-style arrays. I got sick of constantly using a dynamic vector with
I also have one question: is my implementation of
The current "demands" for the C-style array:
This is what I have:
```
/*
Carray - a C++ container that resembles a C-style array very closely
while supporting high-level methods and dynamic memory allocation.
Description:
A Carray is an Random Access Container (http://www.sgi.com/tech/stl/Container.html,
http://www.sgi.com/tech/stl/RandomAccessContainer.html). The number of elements in
a Carray is fixed and can not vary after construction. Memory management is automatic.
Template arguments:
Carray >
T is the type of each element in the array and A is the allocator
for the array. The allocator defaults to std::allocator.
Carray >
Constructors:
Carray(size_t n) - create new Carray with n uninitialized elements
Carray(size_t n, value_type newobj) - create new Carray with n elements initialized to newobj
Carray(const Carray& carray) - copy constructor
Example:
Carray arr(50) - Carray of 50 ints - garbage values
Carray arr(50, int()) - Carray of 50 ints - initialized to default int, 0
Carray supports all of the associated types, members and methods described on these pages:
http://www.sgi.com/tech/stl/Container.html
http://www.sgi.com/tech/stl/ForwardContainer.html
http://www.sgi.com/tech/stl/ReversibleContainer.html
http://w
&vec[0], so I wrote this C-style array container. Please review and give suggestions.I also have one question: is my implementation of
swap correct? My biggest worry is swapping two C-style arrays with different allocators, will this go boom on the deallocation?The current "demands" for the C-style array:
- Static size, so no
resize().
- Implicitly converts to
T*when needed to pass to C functions.
- Support all of
std::vector's methods, as long as they are compatible with the two demands earlier.
This is what I have:
```
/*
Carray - a C++ container that resembles a C-style array very closely
while supporting high-level methods and dynamic memory allocation.
Description:
A Carray is an Random Access Container (http://www.sgi.com/tech/stl/Container.html,
http://www.sgi.com/tech/stl/RandomAccessContainer.html). The number of elements in
a Carray is fixed and can not vary after construction. Memory management is automatic.
Template arguments:
Carray >
T is the type of each element in the array and A is the allocator
for the array. The allocator defaults to std::allocator.
Carray >
Constructors:
Carray(size_t n) - create new Carray with n uninitialized elements
Carray(size_t n, value_type newobj) - create new Carray with n elements initialized to newobj
Carray(const Carray& carray) - copy constructor
Example:
Carray arr(50) - Carray of 50 ints - garbage values
Carray arr(50, int()) - Carray of 50 ints - initialized to default int, 0
Carray supports all of the associated types, members and methods described on these pages:
http://www.sgi.com/tech/stl/Container.html
http://www.sgi.com/tech/stl/ForwardContainer.html
http://www.sgi.com/tech/stl/ReversibleContainer.html
http://w
Solution
It's a subjective point, but declarations of template containers in the standard library don't use the typedefs for the template type for the rest of the declaration. For example,
I'm used to looking for the template parameter in member function declarations, and your use of
Personally, I don't like the implicit conversion to
template
class deque {
public:
typedef T value_type;
//...
void push_front( const T& x );
// not void push_front(const value_type& x);
//...
}I'm used to looking for the template parameter in member function declarations, and your use of
value_type etc. confused me.Personally, I don't like the implicit conversion to
T*. You need to make sure you've thought of all the cases where this could cause unexpected behaviour, e.g. in boolean contexts, arithmetic operators and with ostream::operator<<. In my opinion an extra function call on the conversion is a small price to pay for avoiding cases where code looks right, compiles and does something subtly wrong.Code Snippets
template <class T, class Allocator>
class deque {
public:
typedef T value_type;
//...
void push_front( const T& x );
// not void push_front(const value_type& x);
//...
}Context
StackExchange Code Review Q#2455, answer score: 6
Revisions (0)
No revisions yet.