Skip to content

Commit b8f5d37

Browse files
authored
Merge pull request #239 from elbeno/update-ct-format-docs
📚 Update docs for `ct_format`
2 parents 278719f + 6f91ffc commit b8f5d37

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

docs/ct_format.adoc

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,55 @@ The format string is provided as a template argument, and the arguments to be
1111
formatted as regular function arguments.
1212

1313
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
1515
xref:tuple.adoc#_tuple_hpp[`tuple`] of the format arguments.
1616
[source,cpp]
1717
----
1818
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}}
2020
----
2121

2222
When format arguments are available at compile-time, wrapping them in
2323
`CX_VALUE(...)` means they will get compile-time formatted.
2424
[source,cpp]
2525
----
2626
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}}
2828
----
2929

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...
3131
[source,cpp]
3232
----
3333
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">()
3542
----
3643

3744
Types and compile-time enumeration values are stringified thus:
3845
[source,cpp]
3946
----
4047
enum struct E { value };
4148
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{}}
4350
----
4451

4552
NOTE: Compile-time formatting is done with https://github.com/fmtlib/fmt[fmtlib]
4653
and supports the same formatting DSL. Positional arguments are not supported.
4754

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-
6255
When formatting a compile-time `stdx::format_result`, the strings and argument
6356
tuples are collapsed to a single `stdx::format_result`:
6457

6558
[source,cpp]
6659
----
6760
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}}
6962
7063
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}}
7265
----

docs/ct_string.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ However, for interfacing with legacy functions, a null terminator can be useful.
8484

8585
See https://github.com/intel/compile-time-init-build/tree/main/include/sc[cib
8686
documentation] for details about the cib string constant class.
87+
88+
Sometimes it is useful to wrap a `ct_string` in a type to preserve compile-time
89+
properties. For this, there is `cts_t` and a corresponding user-defined literal.
90+
[source,cpp]
91+
----
92+
using namespace stdx::literals;
93+
94+
constexpr auto s = "hello"_ctst;
95+
// s is a stdx::cts_t<"hello">
96+
----
97+
Think of `cts_t` relating to `ct_string` as `std::integral_constant` relates to `int`.

0 commit comments

Comments
 (0)