What is std::move(), and when should it be used and does it actually move anything?

1. “What is it?” While std::move() is technically a function – I would say it isn’t really a function. It’s sort of a converter between ways the compiler considers an expression’s value. 2. “What does it do?” The first thing to note is that std::move() doesn’t actually move anything. It changes an expression from being … Read more

What is the meaning of ‘const’ at the end of a member function declaration?

When you add the const keyword to a method the this pointer will essentially become a pointer to const object, and you cannot therefore change any member data. (Unless you use mutable, more on that later). The const keyword is part of the functions signature which means that you can implement two similar methods, one … Read more

Rationale of enforcing some operators to be members

The four operators mentioned in the original posting, =, (), -> and [], must indeed be implemented as non-static member functions (by respectively C++98 §13.5.3/1, §13.5.4/1, §13.5.5/1 and §13.5.6/1). Bjarne Stroustrup’s rationale was, as I recall from earlier debates on the subject, to retain some sanity in the language, i.e. having at least some things … Read more

What are the main purposes of std::forward and which problems does it solve?

You have to understand the forwarding problem. You can read the entire problem in detail, but I’ll summarize. Basically, given the expression E(a, b, … , c), we want the expression f(a, b, … , c) to be equivalent. In C++03, this is impossible. There are many attempts, but they all fail in some regard. … Read more

How do I prevent a class from being allocated via the ‘new’ operator? (I’d like to ensure my RAII class is always allocated on the stack.)

All you need to do is declare the class’ new operator private: class X { private: // Prevent heap allocation void * operator new (size_t); void * operator new[] (size_t); void operator delete (void *); void operator delete[] (void*); // … // The rest of the implementation for X // … }; Making ‘operator new’ … Read more