My Project
Loading...
Searching...
No Matches
ConstexprAssert.hpp
1/*
2 A compilation of the following posts:
3 http://stackoverflow.com/questions/18648069/g-doesnt-compile-constexpr-function-with-assert-in-it
4 http://ericniebler.com/2014/09/27/assert-and-constexpr-in-cxx11/
5
6 This file is part of the Open Porous Media project (OPM).
7
8 OPM is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 OPM is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with OPM. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#ifndef OPM_CONSTEXPR_ASSERT_HPP
23#define OPM_CONSTEXPR_ASSERT_HPP
24
25#include <cassert>
26#include <utility>
27
28namespace {
29template<class Assert>
30void constexpr_assert_failed(Assert&& a) noexcept { std::forward<Assert>(a)(); }
31}
32
33// When evaluated at compile time emits a compilation error if condition is not true.
34// Invokes the standard assert at run time.
35#define constexpr_assert(cond) \
36 ((void)((cond) ? 0 : (constexpr_assert_failed([](){ assert(!#cond);}), 0)))
37
38#endif