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 转换,而 Then
和 Else
必须是语法。如果 boost::result_of<proto::when<proto::_, If>(E)>::type::value
为 true
且 E
匹配 Then
;或者,如果 boost::result_of<proto::when<proto::_, If>(E)>::type::value
为 false
且 E
匹配 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::value
为 true
。
// 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>
用作转换时,If
、Then
和 Else
必须是 Proto 转换。当将转换应用于表达式 E
、状态 S
和数据 V
时,如果 boost::result_of<proto::when<proto::_, If>(E,S,V)>::type::value
为 true
,则应用 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_() > > {};