New features in C++ 11/14/17

Content

    • 1 Description
    • 2.1.1 Initializer list
    • 2.1.2 static-assert
    • 2.1.3 constexpr
    • 2.1.4 noexcept

1 Description

2.1.1 Initializer list

An object of const-qualified or reference type must be initialized before the function body of constructor.

Take an example, if a block of code goes like

#include 

class A
{
	int i;
	const int a;
	A(int n);
};

int main()
{
	return 0;
}

A::A(int n)
{
	i = 0;
	a = i;
}

or

#include 

class A
{
	int i;
	int &a;
	A(int n);
};

int main()
{
	return 0;
}

A::A(int n)
{
	i = 0;
	a = i;
}

a compiling error (C2758) will occur, since int &a or const int a is left uninitialized.

Try the following code instead

#include 

class A
{
	int i;
	int &a;
	A(int n);
};

int main()
{
	return 0;
}

A::A(int n) : a(n) 
{
	int i = 0;
 }

2.1.2 static-assert

This idetifier enables assertions during compiling time.
For example, to ensure two structures are of the same size, static-assert can be inserted.

Take the psuedo-code below as an example.

struct A { };
struct B { };

//assert(sizeof(A) == sizeof(B));
static_assert(sizeof(A) == sizeof(B), "Size not equalized.");

Comparing with assert(), static_assert() would enable developers to identify problems at the compilng time.

2.1.3 constexpr

This identifier defines an expression that can be evaluated during compilation.
Unlike const, constexpr can be applied to variables, functions, including constructors.

A constexpr function will return constexpr if its arguments are all constexpr.

constexpr int a (int n) 
{
	return (n * n);
}

constexpr int b = a(1);

A constexpr constructor contains an empty function body, since constexpr variables cannot be changed, and the initialization process is defined in initializer list.

class A 
{
	int m, n;
	constexpr A(int a, int b);
}

constexpr A::A (int a, int b) : m(a), n(b) { };

Comparing to marco definitions, constexpr enables type constriants during compling time. constexpr also promotes optimization for compilers.

2.1.4 noexcept

noexcept specification informs the compiler that the given function will not cause any exception.

According to Exception specifications by Microsoft Docs, the exception behaviours of a function depend on the following factors:

  • Compilation mode
  • extern “C”
  • /EH compiling configuration
  • exception specifications

Explicit exception specifitations are not allowed in C functions. By default, a C function will not throw a catch()exception. However, it may cause a structured exception.

For the record, requirements for compilers vary among different C++ standards.

C++ Standard Version Descriptor Compiler Behaviour Notes for Microsoft Compilers
ISO/IEC 14882:2014 noexcept invoke std::terminate
ISO/IEC 14882:2014 noexcept(true) invoke std::terminate
ISO/IEC 14882:2014 throw() invoke std::unexpected Undefined behaviour. Therefore no specific functions will be invoked, not even std::unexpected or std::terminate.
ISO/IEC 14882:2014 noexcept(false) The function may throw an exception of any type.
ISO/IEC 14882:2014 The function may throw an exception of any type.
ISO/IEC 14882:2014 throw(...) The function may throw an exception of any type.
ISO/IEC 14882:2014 throw(type) The function may throw an exception of type type.
ISO/IEC 14882:2017

when an exception occurs in a noexcept function, std::terminate will be invoked.

啊啊啊啊啊我写不动了谁来替替低产的我换班吧(不是

你可能感兴趣的:(C++)