@@ -11,62 +11,55 @@ The format string is provided as a template argument, and the arguments to be
11
11
formatted as regular function arguments.
12
12
13
13
The result type is a `stdx::format_result` containing two members: the
14
- xref:ct_string.adoc#_ct_string_hpp[`ct_string`] and a
14
+ xref:ct_string.adoc#_ct_string_hpp[`ct_string`] (wrapped in a `cts_t`) and a
15
15
xref:tuple.adoc#_tuple_hpp[`tuple`] of the format arguments.
16
16
[source,cpp]
17
17
----
18
18
auto s = stdx::ct_format<"Hello {} {}">(42, 17);
19
- // s is stdx::format_result{"Hello {} {}"_cts , stdx::tuple{42, 17}}
19
+ // s is stdx::format_result{"Hello {} {}"_ctst , stdx::tuple{42, 17}}
20
20
----
21
21
22
22
When format arguments are available at compile-time, wrapping them in
23
23
`CX_VALUE(...)` means they will get compile-time formatted.
24
24
[source,cpp]
25
25
----
26
26
auto s = stdx::ct_format<"Hello {} {}">(CX_VALUE(42), 17);
27
- // s is stdx::format_result{"Hello 42 {}"_cts , stdx::tuple{17}}
27
+ // s is stdx::format_result{"Hello 42 {}"_ctst , stdx::tuple{17}}
28
28
----
29
29
30
- If there are no runtime arguments, the result is just a `stdx::ct_string`:
30
+ If there are no runtime arguments, the result is a `format_result` with an empty tuple...
31
31
[source,cpp]
32
32
----
33
33
auto s = stdx::ct_format<"Hello {} {}">(CX_VALUE(42), CX_VALUE(17));
34
- // s is "Hello 42 17"_cts
34
+ // s is stdx::format_result{"Hello 42 17"_ctst, stdx::tuple{}}
35
+ ----
36
+ ...and a `format_result` like this without an runtime arguments is implicitly convertible back to a `ct_string`:
37
+ [source,cpp]
38
+ ----
39
+ template <stdx::ct_string S> auto f() { ... };
40
+
41
+ f<stdx::ct_format<"Hello {}">(CX_VALUE(42))>(); // equivalent to f<"Hello 42">()
35
42
----
36
43
37
44
Types and compile-time enumeration values are stringified thus:
38
45
[source,cpp]
39
46
----
40
47
enum struct E { value };
41
48
auto s = stdx::ct_format<"Hello {} {}">(CX_VALUE(int), CX_VALUE(E::value));
42
- // s is "Hello int value"_cts
49
+ // s is stdx::format_result{ "Hello int value"_ctst, stdx::tuple{}}
43
50
----
44
51
45
52
NOTE: Compile-time formatting is done with https://github.com/fmtlib/fmt[fmtlib]
46
53
and supports the same formatting DSL. Positional arguments are not supported.
47
54
48
- `ct_format` is designed for use with a logging backend like the one in
49
- https://github.com/intel/compile-time-init-build. Hence `stdx::format_result`
50
- allows lazy runtime formatting. For the same reason, compile-time formatting can
51
- output string information in a
52
- https://github.com/intel/compile-time-init-build/tree/main/include/sc[suitable
53
- type] rather than as a value. This is done by passing the template as a second
54
- template argument to `ct_format`.
55
-
56
- [source,cpp]
57
- ----
58
- auto s = stdx::ct_format<"{}", sc::string_constant>(CX_VALUE(42));
59
- // s is sc::string_constant<char, '4', '2'>{}
60
- ----
61
-
62
55
When formatting a compile-time `stdx::format_result`, the strings and argument
63
56
tuples are collapsed to a single `stdx::format_result`:
64
57
65
58
[source,cpp]
66
59
----
67
60
constexpr static auto a = stdx::ct_format<"The answer is {}">(42);
68
- // a is stdx::format_result{"The answer is {}", stdx::tuple{42}}
61
+ // a is stdx::format_result{"The answer is {}"_ctst , stdx::tuple{42}}
69
62
70
63
constexpr static auto q = stdx::ct_format<"{}. But what is the question?">(CX_VALUE(a));
71
- // q is stdx::format_result{"The answer is {}. But what is the question?", stdx::tuple{42}}
64
+ // q is stdx::format_result{"The answer is {}. But what is the question?"_ctst , stdx::tuple{42}}
72
65
----
0 commit comments