Boost C++ 库

……世界上最受推崇、设计最精良的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ Coding Standards

结构体模板 if_ - Boost C++ 函数库
PrevUpHomeNext

结构体模板 if_

boost::proto::if_ — 用于根据编译时布尔值的结果选择一个语法或另一个。当用作转换时,proto::if_<> 根据编译时布尔值在两个转换之间进行选择。

提要

// In header: <boost/proto/matches.hpp>

template<typename If, typename Then = proto::_, 
         typename Else = proto::not_<proto::_> > 
struct if_ :  proto::transform<if_<If, Then, Else> > {
  // types
  typedef if_ proto_grammar;

  // member classes/structs/unions
  template<typename Expr, typename State, typename Data> 
  struct impl :  proto::transform_impl< Expr, State, Data > {
    // types
    typedef typename mpl::if_<
      typename boost::result_of<proto::when<proto::_, If>(Expr, State, Data)>::type,
      typename boost::result_of<proto::when<proto::_, Then>(Expr, State, Data)>::type,
      typename boost::result_of<proto::when<proto::_, Else>(Expr, State, Data)>::type
    >::type result_type;

    // public member functions
    result_type operator()(typename impl::expr_param, 
                           typename impl::state_param, 
                           typename impl::data_param) const;
  };
};

描述

proto::if_<If, Then, Else> 用作语法时,If 必须是 Proto 转换,而 ThenElse 必须是语法。如果 boost::result_of<proto::when<proto::_, If>(E)>::type::valuetrueE 匹配 Then;或者,如果 boost::result_of<proto::when<proto::_, If>(E)>::type::valuefalseE 匹配 Else,则表达式类型 E 匹配 proto::if_<If, Then, Else>

模板参数 Then 默认为 proto::_Else 默认为 proto::not_<proto::_>,因此表达式类型 E 匹配 proto::if_<If> 当且仅当 boost::result_of<proto::when<proto::_, If>(E)>::type::valuetrue

// A grammar that only matches integral terminals,
// using is_integral<> from Boost.Type_traits.
struct IsIntegral :
  proto::and_<
    proto::terminal<proto::_>,
    proto::if_< boost::is_integral<proto::_value>()>
  >
{};

proto::if_<If, Then, Else> 用作转换时,IfThenElse 必须是 Proto 转换。当将转换应用于表达式 E、状态 S 和数据 V 时,如果 boost::result_of<proto::when<proto::_, If>(E,S,V)>::type::valuetrue,则应用 Then 转换;否则,应用 Else 转换。

// Match a terminal. If the terminal is integral, return
// mpl::true_; otherwise, return mpl::false_.
struct IsIntegral2 :
  proto::when<
    proto::terminal<_>,
    proto::if_<
      boost::is_integral<proto::_value>(),
      mpl::true_(),
      mpl::false_()
    >
  >
{};


PrevUpHomeNext