]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#return_] |
2 | [section return_] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class C> | |
7 | struct return_; | |
8 | ||
9 | This is a [link parser parser]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`C`] [[link metaprogramming_value template metaprogramming value]]] | |
14 | ] | |
15 | ||
16 | [h1 Description] | |
17 | ||
18 | `return_` accepts every input. The result of parsing is `C`, the remaining | |
19 | string is the input string. | |
20 | ||
21 | [h1 Header] | |
22 | ||
23 | #include <boost/metaparse/return_.hpp> | |
24 | ||
25 | [h1 Expression semantics] | |
26 | ||
27 | For any `c` class, `s` compile-time string and `pos` source position the | |
28 | following are equivalent | |
29 | ||
30 | get_result<return_<c>::apply<s, pos>>::type | |
31 | ||
32 | c | |
33 | ||
34 | get_remaining<return_<c>::apply<s, pos>>::type | |
35 | ||
36 | s | |
37 | ||
38 | get_position<return_<c>::apply<s, pos>>::type | |
39 | ||
40 | pos | |
41 | ||
42 | [h1 Example] | |
43 | ||
44 | #include <boost/metaparse/return_.hpp> | |
45 | #include <boost/metaparse/int_.hpp> | |
46 | #include <boost/metaparse/one_of.hpp> | |
47 | #include <boost/metaparse/start.hpp> | |
48 | #include <boost/metaparse/string.hpp> | |
49 | #include <boost/metaparse/get_result.hpp> | |
50 | ||
51 | #include <type_traits> | |
52 | ||
53 | using namespace boost::metaparse; | |
54 | ||
55 | using default_value = std::integral_constant<int, 13>; | |
56 | ||
57 | using optional_number = one_of<int_, return_<default_value>>; | |
58 | ||
59 | static_assert( | |
60 | get_result< | |
61 | optional_number::apply<BOOST_METAPARSE_STRING("11"), start> | |
62 | >::type::value == 11, | |
63 | "when a number is provided, it is the result of parsing" | |
64 | ); | |
65 | ||
66 | static_assert( | |
67 | get_result< | |
68 | optional_number::apply<BOOST_METAPARSE_STRING(""), start> | |
69 | >::type::value == 13, | |
70 | "when no number is provided, the default value is the result of parsing" | |
71 | ); | |
72 | ||
73 | [endsect] | |
74 |