STL LIST实现 - 2

/* helper class for finding non-const version of a type. Need to have
   something to assign to etc. when testing constant iterators. */

template <class _Tp>
struct _Mutable_trait {
  typedef _Tp _Type;
};
template <class _Tp>
struct _Mutable_trait<const _Tp> {
  typedef _Tp _Type;
};


/* helper function for avoiding compiler warnings about unused variables */
template <class _Type>
void __sink_unused_warning(_Type) { }

template <class _TypeX, class _TypeY>
struct _STL_CONVERT_ERROR {
  static void
  __type_X_is_not_convertible_to_type_Y(_TypeX __x, _TypeY) {
    _TypeY __y = __x;
    __sink_unused_warning(__y);
  }
};


template <class _Type> struct __check_equal { };

template <class _TypeX, class _TypeY>
struct _STL_SAME_TYPE_ERROR {
  static void
  __type_X_not_same_as_type_Y(_TypeX , _TypeY ) {
    __check_equal<_TypeX> t1 = __check_equal<_TypeY>();
  }
};


// Some Functon Object Checks

template <class _Func, class _Ret>
struct _STL_GENERATOR_ERROR {
  static _Ret __generator_requirement_violation(_Func& __f) {
    return __f();
  }
};

template <class _Func>
struct _STL_GENERATOR_ERROR<_Func, void> {
  static void __generator_requirement_violation(_Func& __f) {
    __f();
  }
};


template <class _Func, class _Ret, class _Arg>
struct _STL_UNARY_FUNCTION_ERROR {
  static _Ret
  __unary_function_requirement_violation(_Func& __f,
                                          const _Arg& __arg) {
    return __f(__arg);
  }
};

template <class _Func, class _Arg>
struct _STL_UNARY_FUNCTION_ERROR<_Func, void, _Arg> {
  static void
  __unary_function_requirement_violation(_Func& __f,
                                          const _Arg& __arg) {
    __f(__arg);
  }
};

template <class _Func, class _Ret, class _First, class _Second>
struct _STL_BINARY_FUNCTION_ERROR {
  static _Ret
  __binary_function_requirement_violation(_Func& __f,
                                          const _First& __first,
                                          const _Second& __second) {
    return __f(__first, __second);
  }
};

template <class _Func, class _First, class _Second>
struct _STL_BINARY_FUNCTION_ERROR<_Func, void, _First, _Second> {
  static void
  __binary_function_requirement_violation(_Func& __f,
                                          const _First& __first,
                                          const _Second& __second) {
    __f(__first, __second);
  }
};

以上模板函数功能主要是为了测试各个配件的正确性,每个函数都代表一个相应的STL常用实体,如果

对应实体无法通过与他呼应的模板函数测试就会报错!

/*
  The presence of this class is just to trick EDG into displaying
  these error messages before any other errors. Without the
  classes, the errors in the functions get reported after
  other class errors deep inside the library. The name
  choice just makes for an eye catching error message :)
 */
struct _STL_ERROR {

  template <class _Type>
  static _Type
  __default_constructor_requirement_violation(_Type) {
    return _Type();
  }
  template <class _Type>
  static _Type
  __assignment_operator_requirement_violation(_Type __a) {
    __a = __a;
    return __a;
  }
  template <class _Type>
  static _Type
  __copy_constructor_requirement_violation(_Type __a) {
    _Type __c(__a);
    return __c;
  }
  template <class _Type>
  static _Type
  __const_parameter_required_for_copy_constructor(_Type /* __a */,
                                                  const _Type& __b) {
    _Type __c(__b);
    return __c;
  }
  template <class _Type>
  static _Type
  __const_parameter_required_for_assignment_operator(_Type __a,
                                                     const _Type& __b) {
    __a = __b;
    return __a;
  }
  template <class _Type>
  static _Type
  __less_than_comparable_requirement_violation(_Type __a, _Type __b) {
    if (__a < __b || __a > __b || __a <= __b || __a >= __b) return __a;
    return __b;
  }
  template <class _Type>
  static _Type
  __equality_comparable_requirement_violation(_Type __a, _Type __b) {
    if (__a == __b || __a != __b) return __a;
    return __b;
  }
  template <class _Iterator>
  static void
  __dereference_operator_requirement_violation(_Iterator __i) {
    __sink_unused_warning(*__i);
  }
  template <class _Iterator>
  static void
  __dereference_operator_and_assignment_requirement_violation(_Iterator __i) {
    *__i = *__i;
  }
  template <class _Iterator>
  static void
  __preincrement_operator_requirement_violation(_Iterator __i) {
    ++__i;
  }
  template <class _Iterator>
  static void
  __postincrement_operator_requirement_violation(_Iterator __i) {
    __i++;
  }
  template <class _Iterator>
  static void
  __predecrement_operator_requirement_violation(_Iterator __i) {
    --__i;
  }
  template <class _Iterator>
  static void
  __postdecrement_operator_requirement_violation(_Iterator __i) {
    __i--;
  }
  template <class _Iterator, class _Type>
  static void
  __postincrement_operator_and_assignment_requirement_violation(_Iterator __i,
                                                                _Type __t) {
    *__i++ = __t;
  }
  template <class _Iterator, class _Distance>
  static _Iterator
  __iterator_addition_assignment_requirement_violation(_Iterator __i,
                                                       _Distance __n) {
    __i += __n;
    return __i;
  }
  template <class _Iterator, class _Distance>
  static _Iterator
  __iterator_addition_requirement_violation(_Iterator __i, _Distance __n) {
    __i = __i + __n;
    __i = __n + __i;
    return __i;
  }
  template <class _Iterator, class _Distance>
  static _Iterator
  __iterator_subtraction_assignment_requirement_violation(_Iterator __i,
                                                          _Distance __n) {
    __i -= __n;
    return __i;
  }
  template <class _Iterator, class _Distance>
  static _Iterator
  __iterator_subtraction_requirement_violation(_Iterator __i, _Distance __n) {
    __i = __i - __n;
    return __i;
  }
  template <class _Iterator, class _Distance>
  static _Distance
  __difference_operator_requirement_violation(_Iterator __i, _Iterator __j,
                                              _Distance __n) {
    __n = __i - __j;
    return __n;
  }
  template <class _Exp, class _Type, class _Distance>
  static _Type
  __element_access_operator_requirement_violation(_Exp __x, _Type*,
                                                  _Distance __n) {
    return __x[__n];
  }
  template <class _Exp, class _Type, class _Distance>
  static void
  __element_assignment_operator_requirement_violation(_Exp __x,
                                                      _Type* __t,
                                                      _Distance __n) {
    __x[__n] = *__t;
  }

}; /* _STL_ERROR */

以上的函数主要是为了使错误信息不至于太长,太复杂,从模板函数名就可以看出它们代表的错误,从而

避免了在vc6.0中错误信息有好几行的信息,让人看起来郁闷!

阅读更多
个人分类: STL
上一篇|转帖| C++程序设计之四书五经
下一篇其实在天堂
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭