官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 贡山| 双峰县| 灵璧县| 双江| 米易县| 乌兰县| 永德县| 梧州市| 肃宁县| 盈江县| 樟树市| 阿鲁科尔沁旗| 承德县| 洛隆县| 恩平市| 商都县| 渭南市| 龙海市| 那坡县| 凌源市| 平南县| 双鸭山市| 秀山| 磐安县| 六盘水市| 河西区| 顺昌县| 淄博市| 新乡县| 响水县| 阿鲁科尔沁旗| 信丰县| 南靖县| 闽侯县| 弋阳县| 大安市| 丁青县| 桐柏县| 育儿| 都安| 乃东县|