Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

if_

Synopsis

template <class P, class T, class F>
struct if_;

This is a parser combinator.

Table 38. Arguments


Description

if_ always accepts the input string. The result of parsing is T, when P accepts the input and F otherwise.

Header

#include <boost/metaparse/if_.hpp>

Expression semantics

For any p parser, t and f classes the following are equivalent:

if_<p, t, f>

one_of<last_of<p, return_<t>>, return_<f>>

Example

#include <boost/metaparse/if_.hpp>
#include <boost/metaparse/int_.hpp>
#include <boost/metaparse/string.hpp>
#include <boost/metaparse/start.hpp>
#include <boost/metaparse/get_result.hpp>

#include <type_traits>

using namespace boost::metaparse;

using int11 = std::integral_constant<int, 11>;
using int13 = std::integral_constant<int, 13>;

static_assert(
  get_result<
    if_<int_, int11, int13>::apply<BOOST_METAPARSE_STRING("1234"), start>
  >::type::value == 11,
  "When the int_ parser succeeds, the result of parsing should be 11"
);

static_assert(
  get_result<
    if_<int_, int11, int13>::apply<BOOST_METAPARSE_STRING("foo"), start>
  >::type::value == 13,
  "When the int_ parser fails, the result of parsing should be 13"
);

PrevUpHomeNext