2

Here in this article, the guy is writing an STL compatible prefix tree.

Then we have the selectors and mutators.

/* Selectors */
    const_iterator find(const key_type& key) const;
    size_type size() const;
    size_type max_size() const;
    bool empty() const;
    reference at(const key_type& key);
    const_reference at(const key_type& key) const;

/* Mutators */
    iterator find(const key_type& key);
    std::pair<iterator, bool> insert(const key_type& key, const mapped_type& value);
    std::pair<iterator, bool> insert(const value_type& value);
    iterator insert(const_iterator hint, const value_type& value);
    reference operator[] (const key_type& key);
    void erase(const key_type& key);
    iterator erase(iterator pos);
    void clear();

I though first that they are something like setters and getters. the selectors get some data stored inside or about the container, and the mutators mutate the state of the container. But looks like it's a bit different. for example, there is a selector function called at that returns a non-const reference type (I think this way the content of the container can be changed). What is the difinition of Selectors and Mutators?

StackExchange123
  • 1,871
  • 9
  • 24
  • 3
    The `at` that returns a non-`const` reference seems to be mis-categorized - it should have been in the "mutators" category. Making that change, is the distinction then clear to you, or do you still have a doubt ? – Sander De Dycker Apr 17 '20 at 07:44
  • @SanderDeDycker That makes it clear. Thanks! Expected that but just wanted to make sure. – StackExchange123 Apr 17 '20 at 07:47
  • 2
    [Take a look](https://en.cppreference.com/w/cpp/container/vector) at categories for `std::vector`. They look much more meaningful than these ones. – Evg Apr 17 '20 at 07:48

1 Answers1

2

Those are just names the author of the code chose, not terms that have a formal definition. From context it is clear that they call "Mutators" anything that modifes the container, and "Selectors" anything that allows to get an element from the container or get information on the container. Though, they are not quite consistent, because find does not mutate the container and I would rather list it under "Selectors".

there is a selector function called at that returns a non-const reference type

Perhaps the reasoning was that the reference allows the caller to modify the element in the container, not the container itself.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • isn't modifying elements inside the container consedered a modification to the container it self? – StackExchange123 Apr 17 '20 at 07:51
  • @StackExchange123 both views are valid. As I said, those terms are not about formal definitions, but just what the author chose. They could have called them "Foo" and "Bar" ;) – 463035818_is_not_an_ai Apr 17 '20 at 07:53
  • I suspect the author of the article intended the distinction between the two terms to be basically [`const` member functions](https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration) (ie. all member functions that *can*, and imo *should*, be made `const`) as "selectors" vs. the rest as "mutators". – Sander De Dycker Apr 17 '20 at 07:54
  • @SanderDeDycker might be, but that makes me wonder again, why `find` is not `const` – 463035818_is_not_an_ai Apr 17 '20 at 07:54
  • @idclev463035818 : there is both a `const` and non-`const` version of `find`, depending on the return type. – Sander De Dycker Apr 17 '20 at 07:55
  • @SanderDeDycker oh I missed that. That suggests that your interpretation is to the point – 463035818_is_not_an_ai Apr 17 '20 at 07:56
  • @SanderDeDycker well not really, the non `const` at is also listed as selector. But I wouldn't try to interpret too much into it. The grouping makes some sense and what were the exact decisions to put one method into either of the two groups, only the author knows – 463035818_is_not_an_ai Apr 17 '20 at 07:58
  • I wouldn't call selector and mutator *"just names the author chose"*. These are widely used terms, similarly to getter and setter. – SomeWittyUsername Apr 17 '20 at 08:01
  • @SoemWittyUsername without searching for it I also dont know formal definitions of the terms "getter" and "setter". I mean I know what they refer to, but for exampel I would be ok with calling `find` a "getter" in the most widest sense. Thats what I tried to say, for sure "Mutators" and "Selectors" are not terms that appear in the c++ standard with a clear definition – 463035818_is_not_an_ai Apr 17 '20 at 08:03