C++ vector of arrays

Unfortunately, std::array does not have an initializer list constructor. Indeed, it has no user-defined constructor whatsoever — this “feature” is a leftover from C++03 where omitting all user-defined constructors was the only way to enable the C-style brace initialization. It is IMHO a defect in the current standard. So why doesn’t built-in brace initialization work … Read more

Implementing Move Constructor by Calling Move Assignment Operator

[…] will the compiler be able to optimize away the extra initializations? In almost all cases: yes. Should I always write my move constructors by calling the move assignment operator? Yes, just implement it via move assignment operator, except in the cases where you measured that it leads to suboptimal performance. Today’s optimizer do an … 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

How does “using std::swap” enable Argument-Dependent Lookup (ADL)?

Just wanted to add why this idiom is used at all, which seemed like the spirit of the original question. This idiom is used within many std library classes where swap is implemented. From http://www.cplusplus.com/reference/algorithm/swap/: Many components of the standard library (within std) call swap in an unqualified manner to allow custom overloads for non-fundamental … Read more

A shared recursive mutex in standard C++

Recursive property of the mutex operates with the term “owner“, which in case of a shared_mutex is not well-defined: several threads may have .lock_shared() called at the same time. Assuming “owner” to be a thread which calls .lock() (not .lock_shared()!), an implementation of the recursive shared mutex can be simply derived from shared_mutex: class shared_recursive_mutex: … Read more

What does the & (ampersand) at the end of member function signature mean?

Ref-qualifiers – introduced in C++11 Ref-qualifiers is not C++17 feature (looking at the tag of the question), but was a feature introduced in C++11. struct Foo { void bar() const & { std::cout << “const lvalue Foo\n”; } void bar() & { std::cout << “lvalue Foo\n”; } void bar() const && { std::cout << “const … Read more

Why are std::atomic objects not copyable?

On platforms without atomic instructions (or without atomic instructions for all integer sizes) the types might need to contain a mutex to provide atomicity. Mutexes are not generally copyable or movable. In order to keep a consistent interface for all specializations of std::atomic<T> across all platforms, the types are never copyable.

Why should I delete move constructor and move assignment operator in a singleton?

If you declare a copy constructor (even if you define it as deleted in the declaration), no move constructor will be declared implicitly. Cf. C++11 12.8/9: If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if — X does not … Read more

How do I write a map literal in C++11? [duplicate]

You can actually do this: std::map<std::string, int> mymap = {{“one”, 1}, {“two”, 2}, {“three”, 3}}; What is actually happening here is that std::map stores an std::pair of the key value types, in this case std::pair<const std::string,int>. This is only possible because of c++11’s new uniform initialization syntax which in this case calls a constructor overload … Read more

techhipbettruvabetnorabahisbahis forumueduseduseduseduedueduseduseduedusedu