Is 0 a decimal literal or an octal literal?

Yes, 0 is an Octal literal in C++. As per the C++ Standard: 2.14.2 Integer literals [lex.icon] integer-literal: decimal-literal integer-suffixopt octal-literal integer-suffixopt hexadecimal-literal integer-suffixopt decimal-literal: nonzero-digit decimal-literal digit octal-literal: 0 <——————–<Here> octal-literal octal-digit

Namespace + functions versus static methods on a class

By default, use namespaced functions. Classes are to build objects, not to replace namespaces. In Object Oriented code Scott Meyers wrote a whole Item for his Effective C++ book on this topic, “Prefer non-member non-friend functions to member functions”. I found an online reference to this principle in an article from Herb Sutter: http://www.gotw.ca/gotw/084.htm The … Read more

How to properly add include directories with CMake

Two things must be done. First add the directory to be included: target_include_directories(test PRIVATE ${YOUR_DIRECTORY}) In case you are stuck with a very old CMake version (2.8.10 or older) without support for target_include_directories, you can also use the legacy include_directories instead: include_directories(${YOUR_DIRECTORY}) Then you also must add the header files to the list of your … Read more

How to implement an STL-style iterator and avoid common pitfalls?

http://www.cplusplus.com/reference/std/iterator/ has a handy chart that details the specs of ยง 24.2.2 of the C++11 standard. Basically, the iterators have tags that describe the valid operations, and the tags have a hierarchy. Below is purely symbolic, these classes don’t actually exist as such. iterator { iterator(const iterator&); ~iterator(); iterator& operator=(const iterator&); iterator& operator++(); //prefix increment … Read more

std::vector versus std::array in C++

std::vector is a template class that encapsulate a dynamic array1, stored in the heap, that grows and shrinks automatically if elements are added or removed. It provides all the hooks (begin(), end(), iterators, etc) that make it work fine with the rest of the STL. It also has several useful methods that let you perform … Read more

Difference in make_shared and normal shared_ptr in C++

The difference is that std::make_shared performs one heap-allocation, whereas calling the std::shared_ptr constructor performs two. Where do the heap-allocations happen? std::shared_ptr manages two entities: the control block (stores meta data such as ref-counts, type-erased deleter, etc) the object being managed std::make_shared performs a single heap-allocation accounting for the space necessary for both the control block … Read more

What is the difference between the dot (.) operator and -> in C++? [duplicate]

foo->bar() is the same as (*foo).bar(). The parenthesizes above are necessary because of the binding strength of the * and . operators. *foo.bar() wouldn’t work because Dot (.) operator is evaluated first (see operator precedence) The Dot (.) operator can’t be overloaded, arrow (->) operator can be overloaded. The Dot (.) operator can’t be applied … Read more

C++ Structure Initialization

If you want to make it clear what each initializer value is, just split it up on multiple lines, with a comment on each: address temp_addres = { 0, // street_no nullptr, // street_name “Hamilton”, // city “Ontario”, // prov nullptr, // postal_code };