operator_return_type_traits.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 943 行 · 第 1/2 页

HPP
943
字号
//  operator_return_type_traits.hpp -- Boost Lambda Library ------------------// Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)//// Distributed under the Boost Software License, Version 1.0. (See// accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//// For more information, see www.boost.org#ifndef BOOST_LAMBDA_OPERATOR_RETURN_TYPE_TRAITS_HPP#define BOOST_LAMBDA_OPERATOR_RETURN_TYPE_TRAITS_HPP#include "boost/lambda/detail/is_instance_of.hpp"#include "boost/type_traits/same_traits.hpp"#include "boost/indirect_reference.hpp"#include <cstddef> // needed for the ptrdiff_t#include <iosfwd>  // for istream and ostream#include <iterator> // needed for operator&namespace boost { namespace lambda {namespace detail {// -- general helper templates for type deduction ------------------// Much of the type deduction code for standard arithmetic types from Gary Powelltemplate <class A> struct promote_code { static const int value = -1; };// this means that a code is not defined for A// -- the next 5 types are needed in if_then_else_return // the promotion order is not important, but they must have distinct values.template <> struct promote_code<bool> { static const int value = 10; };template <> struct promote_code<char> { static const int value = 20; };template <> struct promote_code<unsigned char> { static const int value = 30; };template <> struct promote_code<signed char> { static const int value = 40; };template <> struct promote_code<short int> { static const int value = 50; };// ----------template <> struct promote_code<int> { static const int value = 100; };template <> struct promote_code<unsigned int> { static const int value = 200; };template <> struct promote_code<long> { static const int value = 300; };template <> struct promote_code<unsigned long> { static const int value = 400; };template <> struct promote_code<float> { static const int value = 500; };template <> struct promote_code<double> { static const int value = 600; };template <> struct promote_code<long double> { static const int value = 700; };// TODO: wchar_t// forward delcaration of complex.} // namespace detail} // namespace lambda } // namespace boostnamespace std {  template<class T> class complex;}namespace boost { namespace lambda {namespace detail {template <> struct promote_code< std::complex<float> > { static const int value = 800; };template <> struct promote_code< std::complex<double> > { static const int value = 900; };template <> struct promote_code< std::complex<long double> > { static const int value = 1000; };// -- int promotion -------------------------------------------template <class T> struct promote_to_int { typedef T type; };template <> struct promote_to_int<bool> { typedef int type; };template <> struct promote_to_int<char> { typedef int type; };template <> struct promote_to_int<unsigned char> { typedef int type; };template <> struct promote_to_int<signed char> { typedef int type; };template <> struct promote_to_int<short int> { typedef int type; };// The unsigned short int promotion rule is this:// unsigned short int to signed int if a signed int can hold all values // of unsigned short int, otherwise go to unsigned int.template <> struct promote_to_int<unsigned short int>{         typedef                detail::IF<sizeof(int) <= sizeof(unsigned short int),        // I had the logic reversed but ">" messes up the parsing.                unsigned int,                int>::RET type; };// TODO: think, should there be default behaviour for non-standard types?} // namespace detail// ------------------------------------------ // Unary actions ----------------------------// ------------------------------------------ template<class Act, class A>struct plain_return_type_1 {  typedef detail::unspecified type;};template<class Act, class A>struct plain_return_type_1<unary_arithmetic_action<Act>, A> {  typedef A type;};template<class Act, class A> struct return_type_1<unary_arithmetic_action<Act>, A> {   typedef     typename plain_return_type_1<      unary_arithmetic_action<Act>,      typename detail::remove_reference_and_cv<A>::type    >::type type;};template<class A>struct plain_return_type_1<bitwise_action<not_action>, A> {  typedef A type;};// bitwise not, operator~()template<class A> struct return_type_1<bitwise_action<not_action>, A> {  typedef     typename plain_return_type_1<      bitwise_action<not_action>,      typename detail::remove_reference_and_cv<A>::type    >::type type;};// prefix increment and decrement operators return // their argument by default as a non-const referencetemplate<class Act, class A> struct plain_return_type_1<pre_increment_decrement_action<Act>, A> {  typedef A& type;};template<class Act, class A> struct return_type_1<pre_increment_decrement_action<Act>, A> {  typedef     typename plain_return_type_1<      pre_increment_decrement_action<Act>,      typename detail::remove_reference_and_cv<A>::type    >::type type;};// post decrement just returns the same plain type.template<class Act, class A>struct plain_return_type_1<post_increment_decrement_action<Act>, A> {  typedef A type;};template<class Act, class A> struct return_type_1<post_increment_decrement_action<Act>, A> {   typedef     typename plain_return_type_1<      post_increment_decrement_action<Act>,      typename detail::remove_reference_and_cv<A>::type    >::type type;};// logical not, operator!()template<class A> struct plain_return_type_1<logical_action<not_action>, A> {  typedef bool type;};template<class A>struct return_type_1<logical_action<not_action>, A> {  typedef     typename plain_return_type_1<      logical_action<not_action>,      typename detail::remove_reference_and_cv<A>::type    >::type type;};// address of action ---------------------------------------template<class A> struct return_type_1<other_action<addressof_action>, A> {   typedef     typename plain_return_type_1<      other_action<addressof_action>,       typename detail::remove_reference_and_cv<A>::type    >::type type1;  // If no user defined specialization for A, then return the  // cv qualified pointer to A  typedef typename detail::IF<    boost::is_same<type1, detail::unspecified>::value,     typename boost::remove_reference<A>::type*,    type1  >::RET type;};// contentsof action ------------------------------------// TODO: this deduction may lead to fail directly, // (if A has no specialization for iterator_traits and has no// typedef A::reference.// There is no easy way around this, cause there doesn't seem to be a way// to test whether a class is an iterator or not. // The default works with std::iterators.namespace detail {  // A is a nonreference typetemplate <class A> struct contentsof_type {  typedef typename boost::indirect_reference<A>::type type; };  // this is since the nullary () in lambda_functor is always instantiatedtemplate <> struct contentsof_type<null_type> {  typedef detail::unspecified type;};template <class A> struct contentsof_type<const A> {  typedef typename contentsof_type<A>::type type1;  // return a reference to the underlying const type  // the IF is because the A::reference in the primary template could  // be some class type rather than a real reference, hence  // we do not want to make it a reference here either    typedef typename detail::IF<      is_reference<type1>::value,       const typename boost::remove_reference<type1>::type &,      const type1  >::RET type;};template <class A> struct contentsof_type<volatile A> {  typedef typename contentsof_type<A>::type type1;  typedef typename detail::IF<    is_reference<type1>::value,     volatile typename boost::remove_reference<type1>::type &,    volatile type1  >::RET type;};template <class A> struct contentsof_type<const volatile A> {  typedef typename contentsof_type<A>::type type1;  typedef typename detail::IF<    is_reference<type1>::value,     const volatile typename boost::remove_reference<type1>::type &,    const volatile type1  >::RET type;};  // standard iterator traits should take care of the pointer types   // but just to be on the safe side, we have the specializations here:  // these work even if A is cv-qualified.template <class A> struct contentsof_type<A*> {  typedef A& type;};template <class A> struct contentsof_type<A* const> {  typedef A& type;};template <class A> struct contentsof_type<A* volatile> {  typedef A& type;};template <class A> struct contentsof_type<A* const volatile> {  typedef A& type;};template<class A, int N> struct contentsof_type<A[N]> {   typedef A& type; };template<class A, int N> struct contentsof_type<const A[N]> {   typedef const A& type; };template<class A, int N> struct contentsof_type<volatile A[N]> {   typedef volatile A& type; };template<class A, int N> struct contentsof_type<const volatile A[N]> {   typedef const volatile A& type; };} // end detailtemplate<class A> struct return_type_1<other_action<contentsof_action>, A> {   typedef     typename plain_return_type_1<      other_action<contentsof_action>,       typename detail::remove_reference_and_cv<A>::type    >::type type1;  // If no user defined specialization for A, then return the  // cv qualified pointer to A  typedef typename   detail::IF_type<    boost::is_same<type1, detail::unspecified>::value,     detail::contentsof_type<      typename boost::remove_reference<A>::type    >,    detail::identity_mapping<type1>  >::type type;};// ------------------------------------------------------------------// binary actions ---------------------------------------------------// ------------------------------------------------------------------// here the default case is: no user defined versions:template <class Act, class A, class B>struct plain_return_type_2 {  typedef detail::unspecified type; };namespace detail {// error classesclass illegal_pointer_arithmetic{};// pointer arithmetic type deductions ----------------------// value = false means that this is not a pointer arithmetic case// value = true means, that this can be a pointer arithmetic case, but not necessarily is// This means, that for user defined operators for pointer types, say for some operator+(X, *Y),// the deductions must be coded at an earliel level (return_type_2).template<class Act, class A, class B> struct pointer_arithmetic_traits { static const bool value = false; };template<class A, class B> struct pointer_arithmetic_traits<plus_action, A, B> {   typedef typename     array_to_pointer<typename boost::remove_reference<A>::type>::type AP;  typedef typename     array_to_pointer<typename boost::remove_reference<B>::type>::type BP;  static const bool is_pointer_A = boost::is_pointer<AP>::value;  static const bool is_pointer_B = boost::is_pointer<BP>::value;    static const bool value = is_pointer_A || is_pointer_B;  // can't add two pointers.  // note, that we do not check wether the other type is valid for   // addition with a pointer.  // the compiler will catch it in the apply function  typedef typename   detail::IF<    is_pointer_A && is_pointer_B,       detail::return_type_deduction_failure<        detail::illegal_pointer_arithmetic      >,      typename detail::IF<is_pointer_A, AP, BP>::RET  >::RET type; };template<class A, class B> struct pointer_arithmetic_traits<minus_action, A, B> {   typedef typename     array_to_pointer<typename boost::remove_reference<A>::type>::type AP;  typedef typename     array_to_pointer<typename boost::remove_reference<B>::type>::type BP;  static const bool is_pointer_A = boost::is_pointer<AP>::value;  static const bool is_pointer_B = boost::is_pointer<BP>::value;    static const bool value = is_pointer_A || is_pointer_B;  static const bool same_pointer_type =    is_pointer_A && is_pointer_B &&     boost::is_same<      typename boost::remove_const<        typename boost::remove_pointer<          typename boost::remove_const<AP>::type        >::type      >::type,      typename boost::remove_const<        typename boost::remove_pointer<          typename boost::remove_const<BP>::type        >::type      >::type    >::value;  // ptr - ptr has type ptrdiff_t  // note, that we do not check if, in ptr - B, B is   // valid for subtraction with a pointer.  // the compiler will catch it in the apply function  typedef typename   detail::IF<    same_pointer_type, const std::ptrdiff_t,    typename detail::IF<      is_pointer_A,       AP,       detail::return_type_deduction_failure<detail::illegal_pointer_arithmetic>    >::RET  >::RET type; };} // namespace detail   // -- arithmetic actions ---------------------------------------------namespace detail {   template<bool is_pointer_arithmetic, class Act, class A, class B> struct return_type_2_arithmetic_phase_1;template<class A, class B> struct return_type_2_arithmetic_phase_2;template<class A, class B> struct return_type_2_arithmetic_phase_3;} // namespace detail  // drop any qualifiers from the argument types within arithmetic_actiontemplate<class A, class B, class Act> struct return_type_2<arithmetic_action<Act>, A, B>{  typedef typename detail::remove_reference_and_cv<A>::type plain_A;  typedef typename detail::remove_reference_and_cv<B>::type plain_B;  typedef typename     plain_return_type_2<arithmetic_action<Act>, plain_A, plain_B>::type type1;    // if user defined return type, do not enter the whole arithmetic deductions  typedef typename     detail::IF_type<      boost::is_same<type1, detail::unspecified>::value,       detail::return_type_2_arithmetic_phase_1<         detail::pointer_arithmetic_traits<Act, A, B>::value, Act, A, B      >,      plain_return_type_2<arithmetic_action<Act>, plain_A, plain_B>    >::type type;};namespace detail {   // perform integral promotion, no pointer arithmetictemplate<bool is_pointer_arithmetic, class Act, class A, class B> struct return_type_2_arithmetic_phase_1{  typedef typename     return_type_2_arithmetic_phase_2<      typename remove_reference_and_cv<A>::type,      typename remove_reference_and_cv<B>::type    >::type type;};// pointer_arithmetictemplate<class Act, class A, class B> struct return_type_2_arithmetic_phase_1<true, Act, A, B>{  typedef typename     pointer_arithmetic_traits<Act, A, B>::type type;};template<class A, class B>struct return_type_2_arithmetic_phase_2 {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?