I am in the process of learning C++ and have been given a skeleton class to complete. It is a template to create a RingBuffer Container. There is also a template for a ring buffer Iterator which is a random access iterator.
I am having trouble writing the following methods in my RingBuffer class
iterator begin()
const_iterator begin() const
const_iterator cbegin() const
iterator end()
const_iterator end() const
const_iterator cend()
I believe all of these need to make a call the constructor of the Ring Buffer Iterator. However, I am unsure of the correct syntax of how to do this. I have read that In order for a template to be instantiated, every template parameter (type, non-type, or template) must be replaced by a corresponding template argument. Should my implementations look something like this then?
iterator begin()
{
return _RBIterator<T,?,?>::_RBIterator(*this,m_begin); // This refering to the ringbuffer calling RB and M_begin being a pointer to the start of the ring buffer
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
/** @brief Template used to implement RingBuffer<T>::iterator and
* RingBuffer<T>::const_iterator.
*
* This class template represents a random-access iterator for a
* RingBuffer<T>. The nested types RingBuffer<T>::iterator and
* RingBuffer<T>::const_iterator are aliases for instantiations of
* this template; clients should not use this template otherwise than
* via those aliases.
*
* @tparam T Type of object held in the RingBuffer to which this
* iterator type relates.
*
* @tparam Pointer A pointer type, in practice either T* for an
* ordinary iterator or const T* for a const_iterator.
*
* @tparam Reference A reference type, in practice either T& or const
* T&.
*/
template <typename T, typename Pointer, typename Reference>
class _RBIterator {
// The following 'friend' declaration allows one instantiation of
// the _RBIterator template to access private members of another
// instantiation.
template <typename T2, typename P2, typename R2>
friend class _RBIterator;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = Pointer;
using reference = Reference;
_RBIterator(const RingBuffer<T>* ringbuffer, Pointer ptr)
: m_rb(ringbuffer), m_ptr(ptr)
{}
private:
const RingBuffer<T>* m_rb;
// Pointer to the slot of the T array designated by this iterator:
Pointer m_ptr;
|