Should std::atomic be volatile?

Is the compiler free to cache the value of the atomic variable and unroll the loop? The compiler cannot cache the value of an atomic variable. However, since you are using std::memory_order_relaxed, that means the compiler is free to reorder loads and stores from/to this atomic variable with regards to other loads and stores. Also … Read more

Why can’t a constant pointer be a constant expression?

It’s a bit more complicated. In C++03 and C++11, &var is a constant expression if var is a local static / class static or namespace scope variable. This is called an address constant expression. Initializing a class static or namespace scope pointer variable with that constant expression is guaranteed to be done before any code … Read more

tuple vector and initializer_list

The relevant std::tuple constructors are explicit. This means that what you want to do is not possible, since the syntax you want to use is defined in terms of copy initialization (which forbids calling an explicit constructor). In contrast, std::tuple<int, float, char> { 1, 2.2, ‘X’ } uses direct initialization. std::pair does have non-explicit constructors … Read more

Construct std::error_code from errno on POSIX and GetLastError() on Windows

That’s a quality of implementation issue. The const static object returned by std::system_category() is relied upon to perform the mapping from the platform-native error code enumeration to the standard std::error_condition enumeration. Under 17.6.5.14 Value of error codes [value.error.codes]: Implementations for operating systems that are not based on POSIX are encouraged to define values identical to … Read more

A better LOG() macro using template metaprogramming

Here is another expression template which seems to be even more efficient based on some tests that I’ve run. In particular, it avoids creating multiple functions for strings with different lengths by specializing operator<< to use a char * member in the resulting structure. It should also be easy to add other specializations of this … Read more

std::map argument with empty brace-initializers for default argument segfaults in GCC

Looks like this bug was fixed in 4.8.3/4.9.0, the bug report which has a similar example and also seg-faults says: The attached minimal testcase has the following function with default-constructed default argument: void do_something( foo f = {} ) { std::cout << “default argument is at ” << &f << std::endl; } The constructor for … Read more

Erasing elements from unordered_map in a loop

To comply with C++11 you’re unfortunately a bit limited in how you can tackle this. Your options basically boil down to: Iterate over the unordered_map and build a list of keys to delete like so: //std::unordered_map<…> mymap; std::vector<decltype(mymap)::key_type> vec; for (auto&& i : mymap) if (/*compare i*/) vec.emplace_back(i.first); for (auto&& key : vec) mymap.erase(key); Iterate … Read more

Can I use std::current_exception during stack unwinding?

C++ Standard defines current_exception() in section ยง 18.8.5 [propagation] : (emphasis mine) exception_ptr current_exception() noexcept; Returns: An exception_ptr object that refers to the currently handled exception (15.3) or a copy of the currently handled exception, or a null exception_ptr object if no exception is being handled. The referenced object shall remain valid at least as … Read more

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