“std::endl” vs “\n”

The varying line-ending characters don’t matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for. The only difference is that std::endl flushes the output buffer, and ‘\n’ doesn’t. If you don’t want … Read more

How does the compilation/linking process work?

The compilation of a C++ program involves three steps: Preprocessing: the preprocessor takes a C++ source code file and deals with the #includes, #defines and other preprocessor directives. The output of this step is a “pure” C++ file without pre-processor directives. Compilation: the compiler takes the pre-processor’s output and produces an object file from it. … Read more

Why do I have to access template base class members through the this pointer?

Short answer: in order to make x a dependent name, so that lookup is deferred until the template parameter is known. Long answer: when a compiler sees a template, it is supposed to perform certain checks immediately, without seeing the template parameter. Others are deferred until the parameter is known. It’s called two-phase compilation, and … Read more

What are POD types in C++?

POD stands for Plain Old Data – that is, a class (whether defined with the keyword struct or the keyword class) without constructors, destructors and virtual members functions. Wikipedia’s article on POD goes into a bit more detail and defines it as: A Plain Old Data Structure in C++ is an aggregate class that contains … Read more

Why should C++ programmers minimize use of ‘new’?

There are two widely-used memory allocation techniques: automatic allocation and dynamic allocation. Commonly, there is a corresponding region of memory for each: the stack and the heap. Stack The stack always allocates memory in a sequential fashion. It can do so because it requires you to release the memory in the reverse order (First-In, Last-Out: … Read more

Default constructor with empty brackets

Most vexing parse This is related to what is known as “C++’s most vexing parse”. Basically, anything that can be interpreted by the compiler as a function declaration will be interpreted as a function declaration. Another instance of the same problem: std::ifstream ifs(“file.txt”); std::vector<T> v(std::istream_iterator<T>(ifs), std::istream_iterator<T>()); v is interpreted as a declaration of function with … Read more