Avoiding self assignment in std::shuffle

The libstdc++ Debug Mode assertion is based on this rule in the standard, from [res.on.arguments] If a function argument binds to an rvalue reference parameter, the implementation may assume that this parameter is a unique reference to this argument. i.e. the implementation can assume that the object bound to the parameter of T::operator=(T&&) does not … Read more

C++11: How to set seed using

The point of having a seed_seq is to increase the entropy of the generated sequence. If you have a random_device on your system, initializing with multiple numbers from that random device may arguably do that. On a system that has a pseudo-random number generator I don’t think there is an increase in randomness, i.e. generated … Read more

Enum to string in C++11

The longstanding and unnecessary lack of a generic enum-to-string feature in C++ (and C) is a painful one. C++11 didn’t address this, and as far as I know neither will C++14. Personally I’d solve this problem using code generation. The C preprocessor is one way–you can see some other answers linked in the comments here … Read more

Is this change in overload resolution between Clang 3.5 and 3.6 correct or a bug?

I believe Clang is correct to produce this error: The [temp.inst] section of the C++ standard in paragraph 10 says: If a function template or a member function template specialization is used in a way that involves overload resolution, a declaration of the specialization is implicitly instantiated (14.8.3). Forming the implicit conversion sequence necessary to … Read more

Set all bytes of int to (unsigned char)0, guaranteed to represent zero?

C++ 11 I think the pertinent part are 3.9.1/1 In C++11 For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types. Along with 3.9.1/7 The representations of integral types … Read more

How to convert std::chrono::time_point to string

Update for C++20: This can now easily be done in C++20: #include <chrono> #include <format> #include <iostream> #include <string> int main() { using namespace std::chrono_literals; std::chrono::time_point tp = std::chrono::sys_days{2016y/1/16} + 11h + 25min; std::string s = std::format(“{:%Y%m%d%H%M}”, tp); std::cout << s << ‘\n’; } Output: 201601161125 Demo. Original Answer: Howard Hinnant’s free, open source, header-only, … Read more

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