Skip to Content
Mastering C++ Programming
book

Mastering C++ Programming

by Jeganathan Swaminathan
September 2017
Beginner to intermediate
384 pages
8h 4m
English
Packt Publishing
Content preview from Mastering C++ Programming

Simplified static_assert 

The static_assert macro helps identify assert failures during compile time. This feature has been supported since C++11; however, the static_assert macro used to take a mandatory assertion failure message till, which is now made optional in C++17.

The following example demonstrates the use of static_assert with and without the message:

#include <iostream>#include <type_traits>using namespace std;int main ( ) {        const int x = 5, y = 5;        static_assert ( 1 == 0, "Assertion failed" );        static_assert ( 1 == 0 );        static_assert ( x == y );        return 0;}

The output of the preceding program is as follows:

g++-7 staticassert.cpp -std=c++17staticassert.cpp: In function ‘int main()’:staticassert.cpp:7:2: error: static assertion failed: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Multi-Paradigm Programming with Modern C++

Multi-Paradigm Programming with Modern C++

Georgy Pashkov

Publisher Resources

ISBN: 9781786461629Supplemental Content