1

The title says it all, I'll provide some context though. I'm trying to create an iterator for my Dictionary class and I've decided not to nest one inside the other. Therefore the iterator constructors must be private since I don't want them being used without the Dictionary class. At the same time I'm trying to use a technique I just came across called tagging to overload the default constructor depending on where I want the iterator initialized (begin or end of my container).

The point here is not if the way I'm doing it is the best or most optimal but if, as the title says, declaring a explicit private constructor makes sense. I use explicit here because the constructors take one parameter.

template <typename Container>
class DictionaryIterator
{
public:
    using iterator_category = std::forward_iterator_tag;
    using difference_type   = std::ptrdiff_t;
    using value_type        = typename Container::ValueType;
    using pointer           = value_type*;
    using reference         = value_type&;
    // Special case since this container is node-based.
    using node_type         = typename Container::Node;
    // Tag dispatch mechanism
    struct BeginTag {} construct_at_begin;
    struct EndTag {} construct_at_end;
private:
    explicit DictionaryIterator(BeginTag)
    {
        // Do stuff
    }
    explicit DictionaryIterator(EndTag)
    {
        // Do stuff the other way around
    }
};
Eligum
  • 27
  • 7
  • Does this answer your question? [What is the use of making constructor private in a class?](https://stackoverflow.com/questions/2062560/what-is-the-use-of-making-constructor-private-in-a-class) – dtell Feb 04 '21 at 11:16
  • Not really, I do want the constructor to be private, what I'm not sure about is if it has to be declared explicit. – Eligum Feb 04 '21 at 11:19
  • Why do you think this is related to the scoping of a ctor? – dtell Feb 04 '21 at 11:25
  • As far as I know `explicit` is to avoid implicit conversion on the constructor. My reasoning was if the constructor is private and I've friended the container class, so I know only the container will be able to contruct iterators, what's the point of making it explicit as the guide I'm following says? – Eligum Feb 04 '21 at 11:32
  • Making a constructor `explicit` is (like many specifiers) a form of a contract on how your code can (and is intended to) be used. Your reasoning is: I don't need help of the compiler to enforce this contract but will take care of this myself with the intend of holding it anyway. Such situations are almost always doomed because you (or someone else) will eventually break any such *implicit* contract. So just let the compiler help you ;-) Ofc you are technically correct: You don't need this ctor to be explicit but that holds for any explicit declaration of a ctor. – dtell Feb 04 '21 at 11:36
  • Or stated differently: If we all were to write correct code anyways, we wouldn't need a lot of the tools and language specifications modern languages provide. You do you need types? Can't you ensure that you will always use your bits correctly and as intended? Ofc you can't. – dtell Feb 04 '21 at 11:38
  • 1
    Oh I see now, that cleared my doubts thanks! – Eligum Feb 04 '21 at 11:40
  • That is great! Cheers! – dtell Feb 04 '21 at 11:41

0 Answers0