libtranscript
 All Data Structures Functions Variables Enumerations Enumerator Modules
static_assert.h
1 /* Copyright (C) 2011 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 
15 #ifndef STATIC_ASSERT
16 #define STATIC_ASSERT
17 
18 /* The static_assert is achieved by creating a struct definition which contains
19  an array of negative size if the condition is false. To prevent multiple
20  static assertions to define the same struct, the line number on which the
21  assertion name of the struct is made is appended to the name of the struct.
22  However, to do this we need another layer of indirection because the
23  arguments of ## are not expanded before pasting.
24 */
25 #define __static_assert(_condition, _line) struct __static_assert_##_line { int static_assert_failed[_condition ? 1 : -1]; }
26 #define _static_assert(_condition, _line) __static_assert(_condition, _line)
27 #define static_assert(_condition) _static_assert(_condition, __LINE__)
28 
29 #endif