implementing a cpp const iterator crashes and burns - c++

Many posts about const iterators (example), but none in the context of loops like:
for (const auto it: container) { ... }
When I started implementing, I was encouraged by the compiler's complaints about missing begin and end, hoping such complaints can guide me to fill in the missing pieces. I was wrong. The following code compiles fine though it surely lacks the equivalent of both operator++ and operator!=:
#include <iostream>
template<class T> class List { public:
const T *head;
const List<T> *tail;
List(const T *h, const List<T> *t):head(h),tail(t){}
const T *operator*() const { return head; } };
template<class T> const List<T> *begin(const List<T> *l) { return l; }
template<class T> const List<T> *end (const List<T> *l) { return nullptr; }
class Person { public: int age; Person(int a):age(a){} };
typedef List<Person> Persons;
int main(int argc, char **argv) {
Person *p1 = new Person(16);
Person *p2 = new Person(27);
Person *p3 = new Person(38);
Persons *persons = new Persons(p1,
new Persons(p2,
new Persons(p3, nullptr)));
for (const auto p: persons) { std::cout << (*p)->age << "\n"; }
return 0; }
How come this code compiles?

You have not defined ++ and !=, but they are perfectly well-defined for your iterator type, const List<T>*, because is a pointer type. However, the default behavior of ++ does not do what you want, which would be to follow the tail pointer.
To imbue your iterator with special knowledge of how ++ should be implemented, you would want to use a separate, custom iterator class, rather than using a pointer.
I also made some other changes to your code:
The List type is made iterable, not List*. That means the begin and end functions are not defined on pointers, but on the List object itself, and we iterate over *persons rather than persons.
In for (const auto p : *persons), p is a Person, not a pointer.
godbolt.org link
#include <iostream>
template<class T> class List { public:
const T *head;
const List<T> *tail;
List(const T *h, const List<T> *t):head(h),tail(t){}
const T *operator*() const { return head; }
class const_iterator {
const List<T>* cur = nullptr;
public:
explicit const_iterator(const List<T>* list) : cur(list) {}
const_iterator& operator++() {
cur = cur->tail;
return *this;
}
bool operator!=(const const_iterator& other) const {
return cur != other.cur;
}
const T& operator*() const {
return *cur->head;
}
};
const_iterator begin() const {
return const_iterator(this);
}
const_iterator end() const {
return const_iterator(nullptr);
}
};
class Person { public: int age; Person(int a):age(a){} };
typedef List<Person> Persons;
int main(int argc, char **argv) {
Person *p1 = new Person(16);
Person *p2 = new Person(27);
Person *p3 = new Person(38);
Persons *persons = new Persons(p1,
new Persons(p2,
new Persons(p3, nullptr)));
for (const auto p: *persons) {
std::cout << p.age << "\n";
}
return 0;
}

Related

How to display (or loop through) a stack in c++

I am new to c++ and am trying to figure out how to display the contents of my stack after running it through a program.
int main(){
int userInput;
Stack st1,st2;
cin>>userInput;
int i,topStack;
while(userInput!=-9){
while(userInput>0){
st1.push(userInput);
topStack=st1.pop()
st2.push(topStack);
}
if(userInput<0){
for(i=0;i<(-userInput);++i){
st2.pop();
}
}
}
}
I want to see what st2 looks like after I input values. Not quite sure how to do that and/or if it is possible. Resources would be more than appreciated!
Here is the custom class definition:
class ListNode{
public:
int content;
ListNode* pNext;
};
class Stack{
private:
ListNode* top;
public:
Stack();
~Stack();
int pop();
void push(int);
};
Stack::Stack(){
top=nullptr;
}
void Stack::push(int x){
ListNode *newNode;
newNode=new ListNode;
newNode->content=x;
newNode->pNext=top;
top=newNode;
}
int Stack::pop(){
int fR=-17; //empty stack
if(top!=nullptr){
ListNode* newNode;
newNode=top;
top=top->pNext;
fR=newNode->content;
delete newNode;
}
return fR;
}
Stack::~Stack(){
while(top!=nullptr){
pop();
}
}
Here is a fairly minimal implementation of an iterator class to allow viewing the contents of a Stack object.
I've also made some changes in the existing code to be more in line with modern C++ best practices. In particular, you should hardly ever be using raw new and delete directly, so I've updated the code to use std::unique_ptr instead.
stack_iterator.hpp:
#include <iterator>
#include <memory>
#pragma once
class Stack {
private:
struct ListNode {
int content;
std::unique_ptr<ListNode> pNext;
};
public:
Stack() = default;
Stack(const Stack&) = delete;
Stack(Stack&&) = delete;
Stack& operator=(const Stack&) = delete;
Stack& operator=(Stack&&) = delete;
void push(int);
int pop();
class const_iterator {
public:
using difference_type = std::ptrdiff_t;
using value_type = const int;
using pointer = const int*;
using reference = const int&;
using iterator_category = std::forward_iterator_tag;
const_iterator() = delete;
const_iterator(const const_iterator&) = default;
const_iterator& operator=(const const_iterator&) = default;
// Constructor making a const_iterator pointing to a particular
// list node.
const_iterator(ListNode*);
// Pre-increment operator, e.g. called by ++i
const_iterator& operator++();
// Post-increment operator, e.g. called by i++
const_iterator operator++(int);
// Deference operator, e.g. called by *i
reference operator*() const;
// Comparison operators
bool operator==(const const_iterator&) const;
bool operator!=(const const_iterator&) const;
private:
ListNode* pNode;
friend class Stack;
};
// Accessors to create iterators to start and end of stack.
// Note that the stack is traversed from most recent to least
// recent entries. Also note that a pop() operation will invalidate
// any iterator referring to the most recent entry of the stack.
const_iterator begin() const;
const_iterator end() const;
private:
std::unique_ptr<ListNode> top;
};
stack_iterator.cpp:
#include "stack_iterator.hpp"
void Stack::push(int x) {
auto newNode = std::make_unique<ListNode>();
newNode->content = x;
newNode->pNext = std::move(top);
top = std::move(newNode);
}
int Stack::pop() {
static constexpr int retvalOnEmpty = -17;
auto oldTop = std::move(top);
if (oldTop != nullptr) {
top = std::move(oldTop->pNext);
return oldTop->content;
}
return retvalOnEmpty;
}
// pre-increment operator
Stack::const_iterator& Stack::const_iterator::operator++() {
pNode = pNode->pNext.get();
return *this;
}
// post-increment operator
Stack::const_iterator Stack::const_iterator::operator++(int) {
auto ret_iterator = *this;
++(*this);
return ret_iterator;
}
// dereference operator
Stack::const_iterator::reference Stack::const_iterator::operator*() const {
return pNode->content;
}
// comparison operators
bool Stack::const_iterator::operator==(const Stack::const_iterator& other) const {
return this->pNode == other.pNode;
}
bool Stack::const_iterator::operator!=(const Stack::const_iterator& other) const {
return this->pNode != other.pNode;
}
// semi-private constructor making an iterator pointing to a particular ListNode
Stack::const_iterator::const_iterator(ListNode* i_node) :
pNode(i_node)
{ }
Stack::const_iterator Stack::begin() const {
return Stack::const_iterator{top.get()};
}
Stack::const_iterator Stack::end() const {
return Stack::const_iterator{nullptr};
}
Example main program showing how to use the iterators:
#include "stack_iterator.hpp"
#include <iostream>
#include <algorithm>
int main() {
Stack s;
s.push(3);
s.push(5);
s.push(-4);
std::cout << "Contents of stack: ";
for (auto n : s)
std::cout << n << ' ';
std::cout << '\n';
// Alternative
std::cout << "Contents of stack, using ostream_iterator: ";
std::copy(s.begin(), s.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';
return 0;
}
Two approaches using the standard library's containers:
Give up your own implementation of a stack entirely, and using the one available as part of the standard library: std::stack. To display the elements, do something like:
void print(std::ostream& os, std::stack s, char delimiter = ' ')
{
if (not s.empty()) {
std::cout << s.top();
}
while (not s.empty()) {
std::cout << delimiter << s.top();
}
}
since you're passing the stack by value, you're really making a copy of it.
Use your own stack implementation, but implement it over an std::list, and expose the list's iterators (similar to Daniel Schepler's answer but without most of the hard work...).
Of course, it's better to implement an operator<< then a print function, but the principle is the same.

linked list using smart pointers - where is it going wrong?

#include <iostream>
#include<cstddef>
#include<string>
using namespace std;
class spexception
{
protected:
string description;
public:
spexception();
spexception(const string&);
virtual const string& what();
};
template <class T>
class sp
{
private:
T* p;
sp(T*);
public:
sp();
sp(const T&);
sp(int);
T& operator * ();
T* operator -> ();
sp& operator = (const sp&);
sp& operator = (const T&);
sp& operator = (int);
bool operator == (const sp&);
~sp();
};
spexception::spexception() : description("No description.")
{}
spexception::spexception(const string& s) : description(s)
{}
const string& spexception::what()
{
return description;
}
template<class T>
sp<T>::sp()
{
p = NULL;
}
template<class T>
sp<T>::~sp()
{
if(p!=NULL) delete p;
}
template<class T>
sp<T>::sp(const T& t)
{
p = new T(t);
}
template<class T>
sp<T>::sp(int i)
{
if(i!=0) throw spexception("Smart pointer cannot be initialized from a non-zero integer.");
p = NULL;
}
template<class T>
sp<T>& sp<T>::operator = (const sp& q)
{
if(*this==q) return *this;
if(p!=NULL) delete p;
p = q.p;
return *this;
}
template<class T>
sp<T>& sp<T>::operator = (const T& t)
{
p = new T(t);
}
template<class T>
sp<T>& sp<T>::operator = (int i)
{
if(i!=0) throw spexception();
p = NULL;
}
template<class T>
bool sp<T>::operator == (const sp& q)
{
return(p==q.p);
}
template<class T>
T& sp<T>::operator * ()
{
return *p;
}
template<class T>
T* sp<T>::operator -> ()
{
return p;
}
using namespace std;
class node
{
public:
int val;
sp<node> next;
node()
{
val = 5;
}
node(int v) : val(v)
{
cout<<"\nNode with value "<<val<<" created.\n";
}
~node()
{
cout<<"\nNode with value "<<val<<"destroyed.\n";
}
};
class list
{
sp<node> first;
sp<node> last;
public:
list()
{
first = NULL;
last = NULL;
}
void add(int v)
{
if(last==NULL)
{
last = node(v);
first = last;
//last->next = NULL;
}
else
{
last->next = node(v);
//last->next->next = NULL;
last = last->next;
}
}
};
main()
{
list l;
l.add(10);
l.add(20);
l.add(30);
l.add(40);
}
The output is "Node with value 40 destroyed" printed infinite times. According to gdb debugger, the problem happens in the list destructor. According to the gd debugger, there is some node which has a smart pointer pointing to the same node. So when the destructor is called it is supposedly being called infinite times. But I dont see this happening in the code. What exactly is the problem?
EDIT: As molbdnilo pointed out, when the destructor for the 'last'smart pointer is called, an attempt is made to delete a dangling pointer. This should cause a crash. However the program is going into an infinite loop instead. Is this a bug with mingw compiler?
template<class T>
sp<T>& sp<T>::operator = (const sp& q)
{
if(*this==q) return *this;
if(p!=NULL) delete p;
p = q.p;
return *this;
}
In the Above Function The Pointer 'p' gets deleted and next instruction tries to assign a value 'q.p' to the deleted 'p',which can cause problem.
I made changes to above function as given below.
template<class T>
sp<T>& sp<T>::operator = (const sp& q)
{
if(*this==q) return *this;
if(p!=NULL) p = q.p;
return *this;
}
This worked out well.
You have no copy constructor, so the default copy constructor will just copy the pointer, giving you two smart pointer objects that point at the same object.
Similarly, you assignment operator just copies the pointer, giving you two smart pointer objects that point at the same object.
Whenever either of these happens, the destructor for the first smart pointer will delete the object, leaving the other smart pointer dangling. When the second smart pointer is destroyed, it deletes the already deleted pointer again, causing undefined behavior.
When you copy or assign a smart pointer, you need to either also clone the pointed at object, or have a reference count somewhere that tracks how many pointers point at the same object. In this latter case, you only delete the object when the refcount drops to 0.

const and rvalue references

Im trying to write an efficient list and node classes that have minimal code to iterate over them. However I'm having difficulty fulfilling all my criteria. I want this code to be used safely in a multi-threaded environment and so if possible I want all the functions and arguments for iteration to be const to ensure no state is being written to ie its all read only. So I wrote the below classes and iteration code:
template<typename _Type_>
class Node
{
template<typename _Type_> friend class List;
public:
Node() : m_Next(NULL) {}
Node(const _Type_& value) : m_Value(value), m_Next(nullptr()) {}
const _Type_& Get() const { return m_Value; }
Node&& Next() const { return std::move(*m_Next); }
operator _Type_ () const { return m_Value; }
_Type_& operator->() { return m_Value; }
const _Type_& operator->() const { return m_Value; }
operator bool() const { return m_Next != nullptr(); }
private:
_Type_ m_Value;
Node* m_Next;
};
template<typename _Type_>
class List
{
public:
typedef Node<_Type_> Node;
List() : m_Head(NULL) {}
void AddHead( const _Type_& value )
{
Node* pNode = GE_NEW(Node, value);
pNode->m_Next = &m_Head;
m_Head = *pNode;
}
Node&& Head() const { return std::move(m_Head); }
private:
Node m_Head;
};
and the all important iteration code:
RenderTargetList::Node&& renderTarget = m_Targets.Head();
while( renderTarget )
{
if(renderTarget->Format() == format)
{
return renderTarget.Get();
}
renderTarget = renderTarget.Next();
}
But this doesn't compile as:
Node&& Head() const { return std::move(m_Head); }
returns a non const rvalue reference in a const function function ie it has to be:
const Node&& Head() const { return std::move(m_Head); }
instead, but then this doesn't work as the iteration code fails on the assignment:
renderTarget = renderTarget.Next();
because renderTarget must now be defined as:
const RenderTargetList::Node&& renderTarget = m_Targets.Head();
because head returns a const. Basically I seem to be in a right mess of const, references, lvalues and rvalues. Someone please help!
Thanks
Here's the basic version of a list class. Note that the iterator type and the node type are two distinct types. This way, the node type can own the value, and the iterator type can have pointer semantics.
I'll post this as a community wiki, as it's rather a comment than a direct answer to the question.
template<typename Type>
class List
{
private:
// note that for immutable nodes, we could store `Type const`
// and for an immutable list, `Node const* const`
struct Node
{
Type m_Value;
Node* m_pNext;
};
Node* m_pHead;
public:
class const_iterator
{
private:
Node const* m_pNode;
friend List;
const_iterator(Node* p_pNode) : m_pNode(p_pNode) {}
public:
const_iterator() : m_pNode(nullptr) {}
explicit operator bool() const
{ return m_pNode; }
const_iterator Next() const
{ return {m_pNode->m_pNext}; }
Type const& Get() const
{ return m_pNode->m_Value; }
friend bool operator!=(const_iterator const& lhs,
const_iterator const& rhs)
{
return lhs.m_pNode != rhs.m_pNode;
}
};
List() : m_pHead(nullptr) {}
~List()
{
// delete nodes
}
List(List const&) = delete; // needs to be customized
List& operator=(List const&) = delete; // this one, too
// the only function that modifies the list:
void AddHead( Type const& value )
{
Node* pNode = new Node{value, m_pHead};
m_pHead = pNode;
}
const_iterator Head() const
{ return {m_pHead}; }
};
Usage example:
#include <iostream>
int main()
{
List<int> l;
for(int i = 0; i < 10; ++i) l.AddHead(i);
auto it = l.Head();
while(it)
{
std::cout << it.Get() << ", ";
it = it.Next();
}
}
I think you are confused at to the point of const if your goal is thread safety -- const does not give you thread safe code, only considerate and well understood access patterns will give you thread safe code.
I have lost of code which is thread sfae which does not make use of const, and you will need to as well if you want to be able to keep the "AddHead" method which certainly in its current form is not thread safe.
If your code is threaded you will need to add mutex locks around the code where you are accessing shared data which could be modified or read by another thread -- the critical zones -- from your code structure these critical zones could be very short lived, so not much of a penalty.

No appropriate default constructor

This is a learning project so please give any additional advice that comes to mind.
I am trying to learn data structures by re-implementing some STL containers/algorithms and I've started with linked lists. If I try to make a list of a class lacking a default constructor I would get a compiler error of "no appropriate default constructor":
#include "list.h"
#include <list>
class testA
{
private:
int mInt;
public:
testA(int i) : mInt(i) {}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::list<testA> wS; // Fine
wS.push_back(1); // Fine
MTL::list<testA> wM; // 'testA' has no appropriate default constructor
wM.push_back(1);
return 0;
}
The problem is I'm using a dummy node in the list to store a link to the beginning and the end of the list. I use this mainly to get the .end() function to work properly and give a decrement-able iterator to one past the end of the list. When I declare an empty list one of the functions wants the default constructor for the templated type so it can make my dummy node! If no default constructor exists, it gives the error message.
On the positive side, it looks like a couple of STL implementations actually use the dummy head node idea. On the negative side, it looks like they pull some nasty tricks to get around this issue. My visual studio 2010 STL implementation eventually calls raw operator new and delete without calling the constructor. I've basically copied what it has (look for ::operator new and delete) and I get it to compile. Here is the full code:
#include <cassert>
#include <iostream>
namespace MTL
{
template< typename T >
class list
{
private:
// If ListNode is in the private part of list, clients
// can't mess around with ListNodes.
template <typename T>
class ListNode
{
private:
ListNode<T>* p_mNextNode;
ListNode<T>* p_mPreviousNode;
T mNodeVal;
public:
// ListNode() :
// p_mNextNode(0),
// p_mPreviousNode(0) {}
ListNode(T const & aVal) :
p_mNextNode(0),
p_mPreviousNode(0),
mNodeVal(aVal) {}
ListNode<T>* GetNextNode() {return p_mNextNode;}
ListNode<T>* GetPreviousNode() {return p_mPreviousNode;}
void SetNextNode(ListNode<T>* const aNode) {p_mNextNode = aNode;}
void SetPreviousNode(ListNode<T>* const aNode) {p_mPreviousNode = aNode;}
T& GetNodeVal() {return mNodeVal;}
};
public:
class iterator
{
private:
ListNode<T>* mIteratorNode;
public:
iterator() : mIteratorNode(0) {}
iterator(ListNode<T>* aListNode) {mIteratorNode = aListNode;}
T& operator*() {return mIteratorNode->GetNodeVal();}
iterator& operator++() {mIteratorNode = mIteratorNode->GetNextNode(); return *this;}
iterator& operator--() {mIteratorNode = mIteratorNode->GetPreviousNode(); return *this;}
bool operator==(iterator const& aIterator) {return mIteratorNode==aIterator.mIteratorNode;}
bool operator!=(iterator const& aIterator) {return !(mIteratorNode==aIterator.mIteratorNode);}
ListNode<T>* GetNode() {return mIteratorNode;}
ListNode<T>* GetNextNode() {return mIteratorNode->GetNextNode();}
ListNode<T>* GetPreviousNode() {return mIteratorNode->GetPreviousNode();}
};
private:
ListNode<T>* p_mHeadNode;
void insert(ListNode<T>* const aNewNode, iterator& aIterator)
{
ListNode<T>* currentNode = aIterator.GetNode();
ListNode<T>* currentsPreviousNode = currentNode->GetPreviousNode();
currentsPreviousNode->SetNextNode(aNewNode);
aNewNode->SetPreviousNode(currentsPreviousNode);
aNewNode->SetNextNode(currentNode);
currentNode->SetPreviousNode(aNewNode);
}
void eraseNode(ListNode<T>* aListNode)
{
ListNode<T>* previousNode = aListNode->GetPreviousNode();
ListNode<T>* nextNode = aListNode->GetNextNode();
previousNode->SetNextNode(aListNode->GetNextNode());
nextNode->SetPreviousNode(aListNode->GetPreviousNode());
if (p_mHeadNode != aListNode)
{
delete aListNode;
}
}
protected:
public:
list() : p_mHeadNode(static_cast<ListNode<T>*>(::operator new (sizeof(ListNode<T>))))
{
// To get .begin or .end to work immediately after construction
p_mHeadNode->SetNextNode(p_mHeadNode);
p_mHeadNode->SetPreviousNode(p_mHeadNode);
}
list(list const& aList) : p_mHeadNode(static_cast<ListNode<T>*>(::operator new (sizeof(ListNode<T>))))
{
p_mHeadNode->SetNextNode(p_mHeadNode);
p_mHeadNode->SetPreviousNode(p_mHeadNode);
ListNode<T>* pCurrent = (aList.p_mHeadNode)->GetNextNode();
while (pCurrent != aList.p_mHeadNode)
{
this->push_back(pCurrent);
pCurrent = pCurrent->GetNextNode();
}
}
void push_front(T const& aNewVal)
{
ListNode<T>* newNode = new ListNode<T>(aNewVal);
this->insert(newNode,this->begin());
}
void push_back(T const& aNewVal)
{
ListNode<T>* newNode = new ListNode<T>(aNewVal);
this->insert(newNode,this->end());
}
void push_back(ListNode<T>* const aListNode)
{
this->push_back(aListNode->GetNodeVal());
}
void push_front(ListNode<T>* const aListNode)
{
this->push_front(aListNode->GetNodeVal());
}
T& front(){ return p_mHeadNode->GetNextNode()->GetNodeVal(); }
T& back(){ return p_mHeadNode->GetPreviousNode()->GetNodeVal(); }
void pop_front() {this->eraseNode(p_mHeadNode->GetNextNode());}
void pop_back() {this->eraseNode(p_mHeadNode->GetPreviousNode());}
const T& front() const { return (p_mHeadNode->GetNextNode())->GetNodeVal(); }
const T& back() const { return (p_mHeadNode->GetPreviousNode())->GetNodeVal(); }
iterator begin() {return iterator(p_mHeadNode->GetNextNode());}
iterator end() {return iterator(p_mHeadNode);}
iterator insert(iterator aPosition, const T& aVal)
{
assert(0);
return iterator();
}
iterator insert(iterator aPosition, unsigned int n, const T& aVal)
{
ListNode<T>* newNode = 0;
for (unsigned int i = 0; i < n; ++i)
{
newNode = new ListNode<T>(aVal);
this->insert(newNode,aPosition);
++aPosition;
}
return iterator(newNode->GetNextNode());
}
iterator insert(iterator aPosition, iterator aFirst, iterator aLast)
{
assert(0);
return iterator();
}
unsigned int size()
{
unsigned int counter = 0;
ListNode<T>* pCurrent = p_mHeadNode->GetNextNode();
while (pCurrent != p_mHeadNode)
{
++counter;
pCurrent = pCurrent->GetNextNode();
}
return counter;
}
~list()
{
this->clear();
::operator delete(p_mHeadNode);
}
void clear()
{
ListNode<T>* pCurrent = p_mHeadNode->GetNextNode();
ListNode<T>* pNext = pCurrent->GetNextNode();
while (pNext != p_mHeadNode)
{
this->eraseNode(pCurrent);
pCurrent = pNext;
pNext = pCurrent->GetNextNode();
}
// All but the last has been deleted
this->eraseNode(pCurrent);
}
bool empty() {return (p_mHeadNode->GetNextNode() != p_mHeadNode);}
friend std::ostream& operator<<(std::ostream& os, list<T> const& aList)
{
ListNode<T>* pCurrent = (aList.p_mHeadNode)->GetNextNode();
std::cout << "List Contents are:\n";
std::cout << "{";
while (pCurrent != aList.p_mHeadNode)
{
std::cout << pCurrent->GetNodeVal();
pCurrent = pCurrent->GetNextNode();
if (pCurrent != aList.p_mHeadNode)
{
std::cout << ",";
}
}
std::cout << "}\n";
return os;
}
};
};
Surely, there must be a cleaner way to get this to work. I can't seem to split my ListNode into a base class of just previous and next pointers and a derived class of the contained data because GetNodeVal() needs a return type of the templated value. So again I would need at least an appropriate constructor to get a dummy value in the base class. I could not make this pure virtual because then my dummy node can not be instantiated as a base class.
This:
http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.3/stl__list_8h-source.html
version is using static casts which seem to be less nasty but I haven't been able to apply it to my code.
So, how can I get this code to work without calling raw operator new/deletes? And will it be any cleaner?
One option for delayed/optional construction of the value could be:
template <typename T>
class ListNode
{
private:
ListNode<T>* p_mNextNode;
ListNode<T>* p_mPreviousNode;
union {
char dummy;
T mNodeVal;
};
ListNode() :
p_mNextNode(0),
p_mPreviousNode(0),
dummy() {}
ListNode(T const & aVal) :
p_mNextNode(0),
p_mPreviousNode(0),
mNodeVal(aVal) {}
...
};
You will need to explicitly call its destructor, however, since the compiler no longer knows which of the variant members is active.
But it's better to do this for the dummy ListNode inside the List. I guess that the implementation with ::operator new was also special-casing the dummy. Something like this:
template< typename T >
class List
{
private:
class ListNode
{
private:
ListNode* p_mNextNode;
ListNode* p_mPreviousNode;
T mNodeVal;
};
class DummyListNode
{
public:
ListNode* p_mNextNode;
ListNode* p_mPreviousNode;
};
These are both "standard-layout", and by 9.2:
If a standard-layout union contains two or more standard-layout structs that share a common initial sequence, and if the standard-layout union object currently contains one of these standard-layout structs, it is permitted to inspect the common initial part of any of them.
Let's take advantage of that now:
union {
DummyListNode dummy_head_tail;
ListNode head_tail
};
// ... iterators and stuff, using &head_tail as the first and last ListNode*
public:
List() : dummy_head_tail{ nullptr, nullptr } { }
~List() { /* free all nodes, then */ dummy_head_tail->~DummyListNode(); }
};

Definition of operator->() in c++

Please tell me how the operator->() in being defined for the iterator of std::list in order to refer members of the element that is being pointed by an iterator.
EDIT:
The problem is that if you implement like this (Fred Nurk):
template<class T>
struct list {
private:
struct Node { // internal class that actually makes up the list structure
Node *prev, *next;
T data;
};
public:
struct iterator { // iterator interface to the above internal node type
T* operator->() const {
return &_node->data;
}
private:
Node *_node;
}
};
Then when you write:
struct A {
int n;
};
void f() {
list<A> L; // imagine this is filled with some data
list<A>::iterator x = L.begin();
x->n = 42;
}
Then
x->n I understand like x->operator->()n which is equivalent to (A ponter to a)n which is a nonsence. How to understand this part. Some answers tell that it is equivalent to x->operator->()->n; (instead of x->operator->()n) but I don't understand why. Please explain me this.
(hint: It returns a pointer to the element.)
The -> operator behaves as follows:
T->x; // some field
T->foo(); // some function
...is equivalent to:
T.operator->()->x;
T.operator->()->foo();
Note the re-application of -> to whatever is returned.
With many details elided, here is the gist of how it works:
template<class T>
struct list {
private:
struct Node { // internal class that actually makes up the list structure
Node *prev, *next;
T data;
};
public:
struct iterator { // iterator interface to the above internal node type
T* operator->() const {
return &_node->data;
}
private:
Node *_node;
}
};
Thus, given:
struct A {
int n;
};
void f() {
list<A> L; // imagine this is filled with some data
list<A>::iterator x = L.begin();
x->n = 42;
// x.operator->() returns an A*
// which gets -> applied again with "n"
}
Operator -> is implemented differently than the other operators in C++. The operator function is expected to return a pointer, and the -> is applied again to that pointer.
It returns pointer. Just for completeness, here is simple example:
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class SafeVectorIterator
{
vector<T> & _vec;
size_t _pos;
public:
SafeVectorIterator(vector<T> & vec) : _vec(vec), _pos(0) { }
void operator++() { ++_pos; }
void operator--() { --_pos; }
T& operator*() { return _vec.at(_pos); }
T* operator->() { return &_vec.at(_pos); }
};
struct point { int x, y; };
int main()
{
vector<point> vec;
point p = { 1, 2 };
vec.push_back(p);
vec.push_back(p);
SafeVectorIterator<point> it(vec);
++it;
it->x = 8;
cout << (*it).y << '\n';
return 0;
}