#Custom STL container - Const iterator not compiling because of issues with custom iterator class

12 messages · Page 1 of 1 (latest)

sonic breach
#

Hey folks, I'm having some weird issues with the iterator implementation in my STL container. I've tried to create a templated class so I can specialise it for iterator and const_iterator on the container. This is (I hope) everything I need to show:

// Part of the IteratorTemplate class
        template <typename PointerType>
        class IteratorTemplate {
            public:
                using iterator_category = std::contiguous_iterator_tag;
                using value_type = Gapvector::value_type;
                using element_type = value_type;
                using pointer = PointerType;
                using reference = value_type&;
                using difference_type = std::ptrdiff_t;
                using gapvector_pointer = std::conditional<std::is_const_v<PointerType>, const Gapvector*, Gapvector*>::type;

                gapvector_pointer gv;
                PointerType ptr;

                IteratorTemplate() = default; 
                IteratorTemplate(gapvector_pointer self, PointerType input_ptr): gv(self), ptr(input_ptr) {}
// iterator types
        using iterator = IteratorTemplate<pointer>;
        using const_iterator = IteratorTemplate<const_pointer>;
// iterator classes on the STL container
        iterator begin() noexcept { return iterator(this, bufferStart); }
        iterator end() noexcept { return iterator(this, bufferEnd); }
        const_iterator begin() const noexcept { return const_iterator(this, bufferStart); }
        const_iterator end() const noexcept { return const_iterator(this, bufferEnd); }
        const_iterator cbegin() const noexcept { return const_iterator(this, bufferStart); }
        const_iterator cend() const noexcept { return const_iterator(this, bufferEnd); }
oblique rivetBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sonic breach
#

The problem I'm running into is that gapvector_pointer typedef and the begin() that returns a const_iterator. I'm getting this error when I compile trying to call that method

#

I think what this is boiling down to is that the gapvector_pointer type is const when it's expecting not to be const, and I'm not sure how to adjust the constructor or the typedef to allow it to only sometimes be const. Any advice here would be greatly appreciated

ocean crane
sonic breach
#

2nd parameter?

#

Oh, I see, because of std::is_const_v<PointerType>

#

I'm trying to avoid writing two different constructors, trying to keep the template to be generic enough for both, otherwise I might as well have not templated it

odd totem
#

The problem is here:

using gapvector_pointer = std::conditional<std::is_const_v<PointerType>, const Gapvector*, Gapvector*>::type;

We can see in the error message that PointerType = const char*, but

std::is_const_v<const char *> == false

std::is_const_v would expect something like const char * const to be true.
This leads to:

using gapvector_pointer = Gapvector*;

while we expect const Gapvector*.
As a result, you're getting an invalid conversion from the provided this of type const Gapvector* to gapvector_pointer = Gapvector*.

You need to remove the pointer first:

  std::is_const_v<std::remove_pointer_t<const char *>>
= std::is_const_v<const char>
= true
sonic breach
#

Wow, thank you for breaking it down for me as well like that. I think I was 80% of the way but this really helps. I’ll just give that a go now

oblique rivetBOT
#

@sonic breach Has your question been resolved? If so, type !solved :)

sonic breach
#

!solved