Blame view

3rdparty/boost_1_81_0/libs/metaparse/doc/always.qbk 1.54 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
  [#always]
  [section always]
  
  [h1 Synopsis]
  
    template <class P, class T>
    struct always;
  
  This is a [link parser_combinator parser combinator].
  
  [table Arguments
    [[Name] [Type]]
    [[`P`]  [[link parser parser]]]
    [[`T`]  [[link metaprogramming_value template metaprogramming value]]]
  ]
  
  [h1 Description]
  
  It accepts an input if and only if `P` accepts it, but the result of parsing
  will be `T` instead of the result `P` returned.
  
  [h1 Header]
  
    #include <boost/metaparse/always.hpp>
  
  [h1 Expression semantics]
  
  For any `p` parser and `t` class the following are equivalent:
  
    always<p, t>
    
    last_of<p, return_<t>>
  
  [h1 Example]
  
    #include <boost/metaparse/always.hpp>
    #include <boost/metaparse/lit_c.hpp>
    #include <boost/metaparse/start.hpp>
    #include <boost/metaparse/string.hpp>
    #include <boost/metaparse/is_error.hpp>
    #include <boost/metaparse/get_result.hpp>
    
    #include <type_traits>
    
    using namespace boost::metaparse;
    
    using always13 = always<lit_c<'x'>, std::integral_constant<int, 13>>;
    
    static_assert(
      !is_error<always13::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value,
      "always13 should accept x"
    );
    
    static_assert(
      std::is_same<
        get_result<always13::apply<BOOST_METAPARSE_STRING("x"), start>>::type,
        std::integral_constant<int, 13>
      >::type::value,
      "the result of parsing should be the integral_constant type"
    );
    
    static_assert(
      is_error<always13::apply<BOOST_METAPARSE_STRING("y"), start>>::type::value,
      "always13 should reject characters other than x"
    );
  
  [endsect]