Using shared_from_this in templated classes

enable_shared_from_this<manager<Policy>> is a “dependent base” (it’s a base class whose type depends on a template parameter, in this case Policy) so the rules of C++ say that unqualified name lookup doesn’t look in there, you need to say this->shared_from_this() or std::enable_shared_from_this<manage<Policy>>::shared_from_this() to find the member from the dependent base. See http://gcc.gnu.org/wiki/VerboseDiagnostics#dependent_base for more detail and … Read more

Massive CPU load using std::lock (c++11)

On my machine, the following code prints out 10 times a second and consumes almost 0 cpu because most of the time the thread is either sleeping or blocked on a locked mutex: #include <chrono> #include <thread> #include <mutex> #include <iostream> using namespace std::chrono_literals; std::mutex m1; std::mutex m2; void f1() { while (true) { std::unique_lock<std::mutex> … Read more

Why do forwarding references have the same syntax as rvalue references?

I think it happened the other way around. The initial idea was to introduce rvalue-references into the language, meaning that “the code providing the double-ampersand reference does not care about what will happen to the referred-to object”. This permits move semantics. This is nice. Now. The standard forbids constructing a reference to a reference, but … Read more

Where can I use alignas() in C++11?

I think you just placed the alignas in the wrong position. If you move it directly after the identifier, both GCC and Clang are happy and apply the alignment: typedef float aligned_block alignas(16) [4]; typedef float aligned_block [4] alignas(16); this is also true if you use using, where the difference also becomes more apparent. Here … Read more

static_assert on initializer_list::size()

“Initializer lists” are just horrible kludges. Don’t: #include <initializer_list> template<typename T> void Dont(std::initializer_list<T> list) { // Bad! static_assert(list.size() == 3, “Exactly three elements are required.”); } void Test() { Dont({1,2,3}); } Do: template<typename T, std::size_t N> void Do(const T(&list)[N]) { // Good! static_assert(N == 3, “Exactly three elements are required.”); } void Test() { Do({1,2,3}); … Read more

Can you read the value of std::atomic_flag without modifying it?

You cannot read the value of a std::atomic_flag without setting it to true. This is by design. It is not a boolean variable (we have std::atomic<bool> for that), but a minimal flag that is guaranteed lock free on all architectures that support C++11. On some platforms the only atomic instructions are exchange instructions. On such … Read more

Why is char distinct from signed char and unsigned char?

There are three distinct basic character types: char, signed char and unsigned char. Although there are three character types, there are only two representations: signed and unsigned. The (plain)char uses one of these representations. Which of the other two character representations is equivalent to char depends on the compiler. In an unsigned type, all the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)