C++11/C++14 Static assertions and constructor delegation - 2020
"If you're an experienced C++ programmer and are anything like me, you initially
approached C++11 thinking, "Yes, yes, I get it. It's C++, only more so." But as you
learned more, you were surprised by the scope of the changes. auto declarations,
range-based for loops, lambda expressions, and rvalue references change the face of
C++, to say nothing of the new concurrency features. And then there are the
idiomatic changes. 0 and typedefs are out, nullptr and alias declarations are in.
Enums should now be scoped. Smart pointers are now preferable to built-in ones.
Moving objects is normally better than copying them.
- Effective Modern C++ by Scott Meyers
The static_assert declaration tests a software assertion at compile time. This can be especially useful for template code.
Syntax looks like this:
static_assert ( bool_constexpr , string )
where
- bool_constexpr: a constant expression that is contextually convertible to bool.
- string: customized string literal that will appear as compiler error if bool_constexpr is false.
// run-time assert assert(ptr != NULL) // C++ 11 // compile-time assert static_assert(sizeof(void *) == 4, "64-bit is not supported.");
In C++03, constructors of a class are not allowed to call other constructors of that class. Each constructor must construct all of its class members itself or call a common member function:
// C++03 class A { void init() { std::cout << "init()"; } void doSomethingElse() { std::cout << "doSomethingElse()\n"; } public: A() { init(); } A(int a) { init(); doSomethingElse(); } };
C++11 allows constructors to call other peer constructors (known as delegation). This allows constructors to utilize another constructor's behavior with a minimum of added code:
// C++11 class A { void doSomethingElse() { std::cout << "doSomethingElse()\n"; } public: A() { ... } A(int a) : A() { doSomethingElse(); } };
However, this provision comes with a caveat, and wiki talked about this:
C++03 considers an object to be constructed when its constructor finishes executing, but C++11 considers an object constructed once any constructor finishes execution. Since multiple constructors will be allowed to execute, this will mean that each delegating constructor will be executing on a fully constructed object of its own type. Derived class constructors will execute after all delegation in their base classes is complete.
For base-class constructors, C++11 allows a class to specify that base class constructors will be inherited. This means that the C++11 compiler will generate code to perform the inheritance, the forwarding of the derived class to the base class. Note that this is an all-or-nothing feature; either all of that base class's constructors are forwarded or none of them are. Also, note that there are restrictions for multiple inheritance, such that class constructors cannot be inherited from two classes that use constructors with the same signature. Nor can a constructor in the derived class exist that matches a signature in the inherited base class.
Note that in C++03, non-constant data members of classes cannot be initialized at the site of the declaration of those members. They can be initialized only in a constructor. In C++11, now it's allowed:
// C++11 class A { int a = 99 void doSomethingElse() { std::cout << "doSomethingElse()\n"; } public: A() { ... } A(int a) : A() { doSomethingElse(); } };
The code below demonstrates another usage of constructor delegation (a constructor calls another constructor of the same class): copy constructor. As we can see, the copy constructor calls default constructor first, and then initializes the current object member with the member of the object just passed in:
A(const A& b) : A() { m_a = b.m_a; }
C++11/C++14 New Features
initializer_list
Uniform initialization
Type Inference (auto) and Range-based for loop
The nullptr and strongly typed enumerations
Static assertions and Constructor delegation
override and final
default and delete specifier
constexpr and string literals
Lambda functions and expressions
std::array container
Rvalue and Lvalue (from C++11 Thread tutorial)
Move semantics and Rvalue Reference (from C++11 Thread tutorial)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization