What is the order of evaluation in a member initializer list?
It depends on the order of data member declarations in the class. So a_ will be the first one, then b_ will be the second one in your example.
It depends on the order of data member declarations in the class. So a_ will be the first one, then b_ will be the second one in your example.
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
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
Reviewing the basic terminology It’s usually good enough – unless you’re programming assembly – to envisage a pointer containing a numeric memory address, with 1 referring to the second byte in the process’s memory, 2 the third, 3 the fourth and so on…. What happened to 0 and the first byte? Well, we’ll get to … Read more
There are fundamental types and compound types. Fundamental types are the arithmetic types, void, and std::nullptr_t. Compound types are arrays, functions, pointers, references, classes, unions, enumerations, and pointers to non-static members. A cv-unqualified type is any of those types. For any cv-unqualified type, there are three corresponding cv-qualified types: const-qualified – with the const cv-qualifier … Read more
It’s basically the same reason why a bowl of bananas is not a bowl of fruits. If a bowl of bananas were a bowl of fruits, you could put an apple into the bowl, and it would no longer be a bowl of bananas. As long as you only inspect the bowl, the conversion is … Read more
Yes it is possible. First of all you need to decide if you want to accept only the type, or if you want to accept a implicitly convertible type. I use std::is_convertible in the examples because it better mimics the behavior of non-templated parameters, e.g. a long long parameter will accept an int argument. If … Read more
John Zwinck’s essential analysis is spot on: The bug is that you’re using shared_from_this() on an object which has no shared_ptr pointing to it. This violates a precondition of shared_from_this(), namely that at least one shared_ptr must already have been created (and still exist) pointing to this. However, his advice seems completely beside the point … Read more
One way you could do this would be to make the constructors private and only allow construction through a static method that returns a pointer. For example: class Foo { public: ~Foo(); static Foo* createFoo() { return new Foo(); } private: Foo(); Foo(const Foo&); Foo& operator=(const Foo&); };
Herb’s answer (before it was edited) actually gave a good example of a type which shouldn’t be movable: std::mutex. The OS’s native mutex type (e.g. pthread_mutex_t on POSIX platforms) might not be “location invariant” meaning the object’s address is part of its value. For example, the OS might keep a list of pointers to all … Read more