Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <int number1>
- void reportErrorIfLessThan10()
- {
- #if(number1 < 10)
- #error the number is less than 10
- #endif
- }
- int maint(int argc, char**argv)
- {
- reportErrorIfLessThan10<5>();//report an error!
- reportErrorIfLessThan10<12>();//ok
- return 0;
- }
- template<bool> class static_check
- {
- };
- template<> class static_check<false>
- {
- private: static_check();
- };
- #define StaticAssert(test) static_check<(test) != 0>()
- template <int number1>
- void reportErrorIfLessThan10()
- {
- typedef char number1_gt_10[number1 > 10 ? 1 : -1];
- }
- int maint(int argc, char**argv)
- {
- reportErrorIfLessThan10<5>();//report an error!
- reportErrorIfLessThan10<12>();//ok
- return 0;
- }
- #define static_assert(test, message) typedef char static_assert_at_ ## __LINE__[(test) ? 1 : -1];
- template <int number1>
- typename boost::enable_if_c< (number1 >= 10) >::type
- reportErrorIfLessThan10() {
- // ...
- }
- template<bool C, typename T = void>
- struct enable_if {
- typedef T type;
- };
- template<typename T>
- struct enable_if<false, T> { };
- template <int number1>
- void reportErrorIfLessThan10() {
- static_assert(number >= 10, "number must be >= 10");
- }
- template <int number1>
- void reportErrorIfLessThan10() {
- BOOST_STATIC_ASSERT(number >= 10);
- }
- namespace detail {
- /* chooses type A if cond == true, chooses type B if cond == false */
- template <bool cond, typename A, typename B>
- struct Condition {
- typedef A type;
- };
- template <typename A, typename B>
- struct Condition<false, A, B> {
- typedef B type;
- };
- struct number1_greater_than_10;
- }
- template <int number1>
- void reportErrorIfLessThan10() {
- // number1 must be greater than 10
- sizeof( typename detail::Condition< (number1 >= 10),
- char,
- detail::number1_greater_than_10
- >::type );
- }
- template <int N>
- struct helper : helper<N - 1> { };
- template <>
- struct helper<10> { typedef void type; };
- template <>
- struct helper<0> { }; // Notice: missing typedef.
- template <int N>
- typename helper<N>::type error_if_less_than_10() {
- }
- int main() {
- error_if_less_than_10<10>();
- error_if_less_than_10<9>();
- }
- x.cpp:16: error: no matching function for call to 'error_if_less_than_10()'
Add Comment
Please, Sign In to add comment