Blame view

3rdparty/boost_1_81_0/libs/metaparse/doc/optional.qbk 2.31 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  [#optional]
  [section optional]
  
  [h1 Synopsis]
  
    template <class P, class Default = /* unspecified */>
    struct optional;
  
  This is a [link parser_combinator parser combinator].
  
  [table Arguments
    [[Name]   [Type]]
    [[`P`]    [[link parser parser]]]
    [[`Default`] [[link metaprogramming_value template metaprogramming value]]]
  ]
  
  [h1 Description]
  
  It tries parsing the input with `P`. When `P` succeeds, the result of parsing is
  the result of `P`. Otherwise no characters are consumed and the result of
  parsing is `Default`.
  
  [h1 Header]
  
    #include <boost/metaparse/optional.hpp>
  
  [h1 Expression semantics]
  
  For any `p` [link parser parser] and `d`
  [link metaprogramming_value template metaprogramming value]
  
    optional<p, d>
  
  is equivalent to
  
    one_of<p, return_<d>>
  
  [h1 Example]
  
    #include <boost/metaparse/optional.hpp>
    #include <boost/metaparse/start.hpp>
    #include <boost/metaparse/int_.hpp>
    #include <boost/metaparse/middle_of.hpp>
    #include <boost/metaparse/sequence.hpp>
    #include <boost/metaparse/lit_c.hpp>
    #include <boost/metaparse/string.hpp>
    #include <boost/metaparse/get_result.hpp>
  
    #include <boost/mpl/int.hpp>
    #include <boost/mpl/equal.hpp>
    #include <boost/mpl/equal_to.hpp>
    #include <boost/mpl/vector_c.hpp>
    
    using namespace boost::metaparse;
    
    using complex_number =
      sequence<
        // Real
        int_,
  
        // Imaginary
        optional<
          middle_of<lit_c<'+'>, int_, lit_c<'i'>>,
          boost::mpl::int_<0>
        >
      >
    ;
    
    static_assert(
      boost::mpl::equal<
        boost::mpl::vector_c<int, 1, 0>,
        get_result<
          complex_number::apply<BOOST_METAPARSE_STRING("1"), start>
        >::type,
  
        boost::mpl::equal_to<boost::mpl::_, boost::mpl::_>
      >::type::value,
      "No imaginary"
    );
  
    static_assert(
      boost::mpl::equal<
        boost::mpl::vector_c<int, 1, 0>,
        get_result<
          complex_number::apply<BOOST_METAPARSE_STRING("1+0i"), start>
        >::type,
  
        boost::mpl::equal_to<boost::mpl::_, boost::mpl::_>
      >::type::value,
      "0 as imaginary"
    );
  
    static_assert(
      boost::mpl::equal<
        boost::mpl::vector_c<int, 0, 1>,
        get_result<
          complex_number::apply<BOOST_METAPARSE_STRING("0+1i"), start>
        >::type,
  
        boost::mpl::equal_to<boost::mpl::_, boost::mpl::_>
      >::type::value,
      "Non-null imaginary"
    );
  
  [endsect]