constexpr and endianness

New answer (C++20) c++20 has introduced a new standard library header <bit>. Among other things it provides a clean, portable way to check the endianness. Since my old method relies on some questionable techniques, I suggest anyone who uses it to switch to the check provided by the standard library. Here’s an adapter which allows … Read more

C++11 multiple read and one write thread mutex [duplicate]

Pretty close, couple things to note, in c++ for exception safety and readability, IMO, it is good to use RAII locks. What you really need is a shared_mutex like in boost or coming in c++14. std::shared_mutex write; //use boost’s or c++14 // One write, no reads. void write_fun() { std::lock_guard<std::shared_mutex> lock(write); // DO WRITE } … Read more

Why ={} initialization doesn’t work for tuple?

In addition to Praetorian’s correct answer (which I’ve upvoted), I wanted to add a little more information… Post-C++14, the standard has been changed to allow: tuple<int, int> t2={1, 2}; to compile and have the expected semantics. The proposal that does this is N4387. This will also allow constructs such as: tuple<int, int> foo() { return … Read more

Why is std::rotate so fast?

As the commentors already stated, it depends on your Standard Library implementation. But the code that you posted is valid even for forward iterators. As such, it imposes very little requirements (only that these iterators can be incremented and dereferenced). Stepanov’s classic Elements of Programming devotes an entire chapter (10) to rotate and other rearrangement … Read more

What is a “Regular Type” in the context of move semantics?

Summary: For C++11 I would include: move-ctor (noexcept) move-assign (noexcept) total ordering (operator<() for natural total order and std::less<> if a natural total order does not exist). hash<> And would remove: swap() (non-throwing) – replaced by move operations. Commentary Alex revisits the concept of a regular type in Elements of Programming. In fact, much of … Read more

c++ lambda capture by value

That’s because the variable is captured by value (i.e. copied) only once, when you define the lambda. It’s not “updated” as you may believe. The code is roughly equivalent to: #include <iostream> int x = 0; struct Lambda { int _internal_x; // this is used to “capture” x ONLY ONCE Lambda(): _internal_x(x) {} // we … Read more

Resolution of std::chrono::high_resolution_clock doesn’t correspond to measurements

I’m going to guess you are using Visual Studio 2012. If not, disregard this answer. Visual Studio 2012 typedef‘s high_resolution_clock to system_clock. Sadly, this means it has crappy precision (around 1 ms). I wrote a better high-resolution clock which uses QueryPerformanceCounter for use in Visual Studio 2012… HighResClock.h: struct HighResClock { typedef long long rep; typedef std::nano period; typedef std::chrono::duration<rep, … Read more

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