Quick C++ Tips

Surbhi Jain
3 min readAug 23, 2019

Here are some quick C++ tips for C++ enthusiasts cum developers which you can definitely use in your code practices. I have learnt them over my practicing and coding in c++.

Some C++ code tips :

  • Use constexpr instead of macros, they are safer.
  • In a C++ class, public followed by protected followed by private for code readability
  • Prefer at() function of vector rather than [] because at() function also checks the bounds of the vector index supplied to at() function.
  • Always check in case of dynamic_cast pointer to pointer cast. Whether it has returned NULL or not.
  • If you want to give stl container some default value like in constructor, do this -

void fun(vector<int> v = vector<int>())

  • In case of converting an rvalue reference to lvalue reference,

> use std::forward

> use std::move

> use explicit type casting to make temporary variable which will be rvalue. (type)(variable_name)

  • Possible disadvantages for using templates can be

> Overuse of this technique can make it take longer to compile your program, and can bloat the size of your program, wasting space and making it take longer to start your program at runtime.

  • Polymorphism with templates

http://davekilian.com/cpp-type-erasure.html

Answer : Before C++11 introduced unrestricted unions, Any class if it has its constructor/destructor defined, we can’t make its object as the part of the union.

But C++11 Introduced unrestricted unions where we can declare the constructor inside the class but for each type, we need to add the constructor in the union also. If I declare any constructor then it wont accept that.

class A { int b;}

A class ka object we can take in union but if it is

class A { int b; public : A(){}}

It wont work.

For e.g. std::vector class has constructors overloaded, so it can’t be taken inside the union.

  • Use of placement new
  • Special member functions in C++
  • Default constructor if no other constructor is explicitly declared.
  • Copy constructor if no move constructor and move assignment operator are explicitly declared.

If a destructor is declared generation of a copy constructor is deprecated (C++11, proposal N3242]).

Move constructor if no copy constructor, copy assignment operator, move assignment operator and destructor are explicitly declared.

Copy assignment operator if no move constructor and move assignment operator are explicitly declared.

If a destructor is declared generation of a copy assignment operator is deprecated.

Move assignment operator if no copy constructor, copy assignment operator, move constructor and destructor are explicitly declared.

Destructor

  • Types of casting in C++

> Static_cast

>dynamic_cast

>reinterpret_cast

>const_cast

Link with nice explanation : https://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast

  • Clone pattern in C++ is used to know the type of actual derived instance which will be lost if we would be passing the derived instance to the base pointer.
  • std::initializer_list<int>
  • Optimization by declaring const :

Compiler can optimize away this const by not providing storage to this variable rather add it in symbol table. So, subsequent read just need indirection into the symbol table rather than instructions to fetch value from memory.

--

--

Surbhi Jain

Engineer @ Google | Technology Enthusiast | IIT Delhi | Computer Science