Suppose that I am writing an iterator and const_iterator for singly linked list.
Suppose that I have a following classes:
template <typename T>
struct Node
{
T value;
Node* next;
}
template <typename T>
struct NodePtr
{
private:
Node<T>* node;
public:
T& operator*() { return node->value; }
const T& operator*() const { return node->value; }
// Ommitted increment, comparison and so on...
}
class Iterator<T, bool isConst>
{
private: NodePtr<T> nodePtr;
using Reference = std::conditional_t<isConst, const T&, T&>;
Reference operator*() { return *nodePtr; }
const Reference operator*() const { return *nodePtr; }
// Ommited
}
My question is whether it is possible somehow to replace the lines
Reference operator*() { return node->value; }
const Reference operator*() const { return node->value; }
with a single definition (perhaps using the template argument isConst) and have the const specifier deduced by the compiler? I want to have * to be const T& operator*() const when isConst = true and have both versions when isConst = false. Is it possible? If it is - then how to do it?