Created
January 19, 2017 14:52
-
-
Save Bueddl/2e4dea884982c22718ceafbebbd75c5f to your computer and use it in GitHub Desktop.
Static assert for C++98
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<bool expr> | |
struct static_assert; | |
template<> | |
struct static_assert<true> | |
{ | |
static const char test; | |
}; | |
struct false_type | |
{ | |
static const bool value = false; | |
}; | |
struct true_type | |
{ | |
static const bool value = true; | |
}; | |
template<int A, int B> | |
struct _n_is_equal : false_type{}; | |
template<int A> | |
struct _n_is_equal<A, A> : true_type{}; | |
template<class T, int N> | |
struct check_sizeof : _n_is_equal<sizeof(T), N>{}; | |
int main() | |
{ | |
static_assert<check_sizeof<char, 1>::value>::test; | |
static_assert<check_sizeof<short, 2>::value>::test; | |
static_assert<check_sizeof<int, 4>::value>::test; | |
static_assert<check_sizeof<long, 4>::value>::test; // ERROR: long is 4 for 64-Bit | |
static_assert<check_sizeof<long long, 8>::value>::test; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment