<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3651: Unspecified lifetime guarantees for the format string</title>
<meta property="og:title" content="Issue 3651: Unspecified lifetime guarantees for the format string">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3651.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#New">New</a> status.</em></p>
<h3 id="3651"><a href="lwg-active.html#3651">3651</a>. Unspecified lifetime guarantees for the format string</h3>
<p><b>Section:</b> 28.5 <a href="https://wg21.link/format">[format]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Barry Revzin <b>Opened:</b> 2021-12-08 <b>Last modified:</b> 2022-01-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#format">active issues</a> in [format].</p>
<p><b>View all other</b> <a href="lwg-index.html#format">issues</a> in [format].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Is this program guaranteed to be valid:
</p>
<blockquote><pre>
#include &lt;format&gt;
#include &lt;algorithm&gt;

struct Thing { };

template &lt;>
struct std::formatter&lt;Thing> {
  std::string_view spec;

  constexpr auto parse(std::format_parse_context&amp; ctx) {
    auto end = std::find(ctx.begin(), ctx.end(), '}');
    spec = std::string_view(ctx.begin(), end);
    return end;
  }

  auto format(Thing, std::format_context&amp; ctx) {
    return std::ranges::copy(spec, ctx.out()).out;
  }
};

int main() {
  std::print("{:lwg issue}\n",  Thing{});
}
</pre></blockquote>
<p>
In <code>parse()</code>, the formatter for <code>Thing</code> holds onto a string view of its specifiers. 
And then in <code>format()</code>, it just prints them. I don't think we say anywhere that this works. 
Does this code print <code>"lwg issue"</code> because there's no issue or does it print some garbage 
memory somewhere because there is one?
<p/>
libfmt's implementation internally stores <code>string_view</code>'s into the format string (for named 
argument support), which implies that it should work. But it'd be nice to come out and say that.
</p>

<p><i>[2022-01-30; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
<br/>
"Presumably we need to say in [formatter.requirements] that
[<code>pc.begin()</code>, <code>pc.end()</code>)
is guaranteed to be a valid range until the
next call to <code>parse()</code> or <code>f</code> is destroyed,
whichever comes first."
</p>



<p id="res-3651"><b>Proposed resolution:</b></p>





</body>
</html>
