Concurrency: Atomic and volatile in C++11 memory model

Firstly, volatile does not imply atomic access. It is designed for things like memory mapped I/O and signal handling. volatile is completely unnecessary when used with std::atomic, and unless your platform documents otherwise, volatile has no bearing on atomic access or memory ordering between threads. If you have a global variable which is shared between … Read more

Does C++11 allow vector?

No, I believe the allocator requirements say that T can be a “non-const, non-reference object type”. You wouldn’t be able to do much with a vector of constant objects. And a const vector<T> would be almost the same anyway. Many years later this quick-and-dirty answer still seems to be attracting comments and votes. Not always … Read more

Legality of COW std::string implementation in C++11

It’s not allowed, because as per the standard 21.4.1 p6, invalidation of iterators/references is only allowed for — as an argument to any standard library function taking a reference to non-const basic_string as an argument. — Calling non-const member functions, except operator[], at, front, back, begin, rbegin, end, and rend. For a COW string, calling … Read more

Optimizing away a “while(1);” in C++0x

To me, the relevant justification is: This is intended to allow compiler transfor- mations, such as removal of empty loops, even when termination cannot be proven. Presumably, this is because proving termination mechanically is difficult, and the inability to prove termination hampers compilers which could otherwise make useful transformations, such as moving nondependent operations from … Read more

Why can I not push_back a unique_ptr into a vector?

You need to move the unique_ptr: vec.push_back(std::move(ptr2x)); unique_ptr guarantees that a single unique_ptr container has ownership of the held pointer. This means that you can’t make copies of a unique_ptr (because then two unique_ptrs would have ownership), so you can only move it. Note, however, that your current use of unique_ptr is incorrect. You cannot … Read more

Is std::unique_ptr required to know the full definition of T?

Adopted from here. Most templates in the C++ standard library require that they be instantiated with complete types. However shared_ptr and unique_ptr are partial exceptions. Some, but not all of their members can be instantiated with incomplete types. The motivation for this is to support idioms such as pimpl using smart pointers, and without risking … Read more