HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

RingQueue implementation using std::array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
stdarrayringqueueusingimplementation

Problem

I've recently finished implementing a ring queue based on std::array in C++11. I tried to write the code as if I were going to submit it to the C++ Standard committee to have a RingQueue as part of the standard while keeping the implementation as simple and fast as possible. Please let me know if there are any changes I should make or any bugs you spot. I've tested the ring queue pretty well and I couldn't find anything outstandingly buggy/wrong about it.

```
#ifndef __RING_QUEUE_H__
#define __RING_QUEUE_H__

#include

template
class RingQueue {
public:
typedef T value_type;
typedef const T const_value_type;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef std::array Container;
typedef typename Container::iterator iterator;
typedef typename Container::const_iterator const_iterator;
typedef typename Container::reverse_iterator reverse_iterator;
typedef typename Container::const_reverse_iterator const_reverse_iterator;

RingQueue(): p(0) {}
~RingQueue(){}
RingQueue(const RingQueue&) = default;
RingQueue& operator=(const RingQueue&) = default;

inline reference operator[](size_t i) { return arr[i]; }
inline const_reference operator[](size_t i) const { return arr[i]; }

inline reference at(size_t i) { return arr.at(i); }
inline const_reference at(size_t i) const { return arr.at(i); }

inline reference front() { return arr.front(); }
inline const_reference front() const { return arr.front(); }

inline reference back() { return arr.back(); }
inline const_reference back() const { return arr.back(

Solution

A few minor stylistic bits, adding to what other reviews already mentioned:

-
__RING_QUEUE_H__ include guard uses the double underscore prefix, which is reserved for implementation and Standard Library names. Refer to this SO question for a complete listing of potentially reserved named and prefixes.

-
Technically, size_t is a member of namespace std in C++, e.g.: std::size_t. It happens to exist in the global scope as well because most implementations use the same header file for both C and C++, so the C header exposes all types globally. This is not a requirement, however, so for better portability your should consider fully qualifying the name.

-
~RingQueue(){}: The empty destructor is not necessary. You only need to provide one if: the class needs some customized cleanup; if you need a virtual destructor for inheritance; if you need to make the destructor private or protected. Otherwise, omit the declaration and let the compiler supply a default for you.

-
inline is not necessary when a class method is directly defined inside the class body in the header file. In this case, it serves no purpose but to make your code more verbose.

-
I would personally use shorter lines. Put the method ret name(parameters) part in its own line, with the { } body on the next following lines.

-
You might consider declaring functions as noexcept where applicable, if you plan on making this as Standard compliant as possible.

-
max_size() is a constexpr method in std::array. It just returns some predefined constant. You can apply that to your wrapper method as well.

Context

StackExchange Code Review Q#96117, answer score: 12

Revisions (0)

No revisions yet.