Why are the conditions for an implicitly-defined move constructor/assignment operator different than for copy operations?

I believe backwards compatibility plays a big part here. If the user defines any of the “Rule of three” functions (copy ctor, copy assignment op, dtor), it can be assumed the class does some internal resource management. Implicitly defining a move constructor could suddenly make the class invalid when compiled under C++11. Consider this example: … Read more

How can I create an atomic enum in C++?

You can create an atomic enum like this: #include <atomic> enum Decision {stay,flee,dance}; std::atomic<Decision> emma_choice {stay}; // emma_choice is atomic You can also do the same thing with enum classes: #include <atomic> enum class Decision {stay,flee,dance}; std::atomic<Decision> emma_choice {Decision::stay}; // emma_choice is atomic

why do i need to use piecewise_construct in map::emplace for single arg constructors of noncopyable objects?

As far as I can tell, the issue isn’t caused by map::emplace, but by pair‘s constructors: #include <map> struct A { A(int) {} A(A&&) = delete; A(A const&) = delete; }; int main() { std::pair<int, A> x(1, 4); // error } This code example doesn’t compile, neither with coliru’s g++4.8.1 nor with clang++3.5, which are … Read more

C++ function types

Here’s the relevant paragraph from the Standard. It pretty much speaks for itself. 8.3.5/10 A typedef of function type may be used to declare a function but shall not be used to define a function (8.4). Example: typedef void F(); F fv; // OK: equivalent to void fv(); F fv { } // ill-formed void … Read more

What is the purpose of std::common_type?

std::common_type was introduced for use with std::duration — if you add a std::duration<int> and a std::duration<short> then the result should be std::duration<int>. Rather than specifying an endless stream of allowed pairings, the decision was made to delegate to a separate template which found the result using the core language rules applicable to the ?: arithmetic-if … Read more

dummy() function – What is that supposed to be?

Let’s simplify the declaration a bit by using simpler types and expressions. We’ll use int instead of std::function<void(int)>, 42 instead of the lambda, and f += 1 instead of f(3): int f{42}, dummy((f += 1, 0)); To make it even more obvious, we can also use braces instead of parentheses for the second initialisation: int … Read more

Where in the standard are functions returning functions disallowed?

From [dcl.fct], pretty explicitly: Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. There shall be no arrays of functions, although there can be arrays of pointers to functions. With C++11, you probably just want: std::function<int()> f(); … Read more

techhipbettruvabetnorabahisbahis forumuedusedusedusedusedusedueduedueduedu