- Mastering C++ Programming
- Jeganathan Swaminathan
- 216字
- 2021-07-02 18:28:46
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++17
staticassert.cpp: In function ‘int main()’:
staticassert.cpp:7:2: error: static assertion failed: Assertion failed
static_assert ( 1 == 0, "Assertion failed" );
staticassert.cpp:8:2: error: static assertion failed
static_assert ( 1 == 0 );
From the preceding output, you can see that the message, Assertion failed, appears as part of the compilation error, while in the second compilation the default compiler error message appears, as we didn't supply an assertion failure message. When there is no assertion failure, the assertion error message will not appear as demonstrated in static_assert ( x == y ). This feature is inspired by the C++ community from the BOOST C++ library.
- JavaScript百煉成仙
- Python程序設計教程(第2版)
- SoapUI Cookbook
- Full-Stack React Projects
- Python面向對象編程:構建游戲和GUI
- 數據結構習題解析與實驗指導
- Protocol-Oriented Programming with Swift
- Learning R for Geospatial Analysis
- 用案例學Java Web整合開發
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- Fast Data Processing with Spark(Second Edition)
- Python自然語言理解:自然語言理解系統開發與應用實戰
- Learning Ionic
- jQuery從入門到精通(微課精編版)
- Maven for Eclipse