What are the stages of compilation of a C++ program?

Are the stages of compilation of a C++ program specified by the standard? Yes and no. The C++ standard defines 9 “phases of translation”. Quoting from the N3242 draft (10MB PDF), dated 2011-02-28 (prior to the release of the official C++11 standard), section 2.2: The precedence among the syntax rules of translation is specified by … Read more

Is C/C++ one language or two languages?

C++ diverged from C in 1982-1983, and that’s a long time in computer years. But, there are many C libraries with C++ compatibility, including the C standard library itself, and a steady stream of programs are ported across from C to C++. Many C programmers only know or use the features that are compatible with … Read more

Is it necessary to std::move in a return statement, and should you return rvalue references?

First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); The first example returns a temporary which is caught by rval_ref. That temporary will have its life extended beyond the rval_ref definition and you can use it as if you had caught it by value. This is very similar to … Read more

What are sequence points, and how do they relate to undefined behavior?

C++98 and C++03 This answer is for the older versions of the C++ standard. The C++11 and C++14 versions of the standard do not formally contain ‘sequence points’; operations are ‘sequenced before’ or ‘unsequenced’ or ‘indeterminately sequenced’ instead. The net effect is essentially the same, but the terminology is different. Disclaimer : Okay. This answer … Read more

What is a lambda expression, and when should I use one?

The problem C++ includes useful generic functions like std::for_each and std::transform, which can be very handy. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function. #include <algorithm> #include <vector> namespace { struct f { void operator()(int) { // do something … Read more

Meaning of default initialization changed in C++11?

The final effects are almost the same. In C++03, the use of default-initialize was restricted to non-POD class type, so the last point never applied. In C++11, the standard simplifies the wording by eliminating the condition with regards to where default-initialization was used, and changes the definition of default-initialization to cover all of the cases … Read more

How to handle constructors that must acquire multiple resources in an exception safe manner

Is there a better way? YES C++11 delivers a new feature called delegating constructors which deals with this situation very gracefully. But it is a bit subtle. The problem with throwing exceptions in constructors is to realize that the destructor of the object you’re constructing doesn’t run until the constructor is complete. Though the destructors … Read more

Lifetime of temporaries

A temporary object is destroyed when the full-expression that lexically contains the rvalue whose evaluation created that temporary object is completely evaluated. Let me demonstrate with ASCII art: ____________________ full-expression ranges from ‘b’ to last ‘)’ bar( foo().c_str() ); ^^^^^ ^ | | birth funeral

error code: 521