39 lines
1.3 KiB
Plaintext
39 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Calls to ``std::format`` and ``std::vformat`` can receive either format strings whose replacement fields are fully indexed or fully non-indexed.
|
|
|
|
Explicitly indexing replacement fields is useful when arguments are not used in order or are used multiple times. Implicit indexing has the advantage of being terse and simple.
|
|
|
|
Using explicit indexing in a context where implicit indexing would have the same behavior is more verbose and error-prone. It might confuse the reader, who might expect the arguments not to be used in order.
|
|
|
|
This rule raises an issue on explicit indexing that should be replaced by implicit indexing.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,cpp]
|
|
----
|
|
std::string printRectangle(Rectangle const& r) {
|
|
return std::format("[{0},{1}]:({2},{3})", r.x, r.y, r.width, r.height); // Noncompliant
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,cpp]
|
|
----
|
|
std::string printRectangle(Rectangle const& r) {
|
|
return std::format("[{},{}]:({},{})", r.x, r.y, r.width, r.height); // Compliant
|
|
}
|
|
----
|
|
|
|
=== Exceptions
|
|
|
|
Format strings with nested replacement fields are not considered by this rule as their order is less intuitive.
|
|
|
|
[source,cpp]
|
|
----
|
|
std::string printNDigits() {
|
|
return std::format("{0:*<{1}} {2:*>{3}}", 10, 2, 11, 3); // Compliant
|
|
}
|
|
----
|