<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>Untitled</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  </style>
  <style type="text/css">pre.sourceCode{border:1px solid #444;padding:12px;border-radius:6px}html{font-size:100%;overflow-y:scroll;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{color:#444;font-family:georgia,palatino,palatino linotype,times,times new roman,serif;font-size:12px;line-height:1.7;padding:1em;margin:auto;max-width:54em;background:#fefefe}a{color:#0645ad;text-decoration:none}a:visited{color:#0b0080}a:hover{color:#06e}a:active{color:#faa700}a:focus{outline:thin dotted}*::-moz-selection{background:rgba(255,255,0,.3);color:#000}*::selection{background:rgba(255,255,0,.3);color:#000}a::-moz-selection{background:rgba(255,255,0,.3);color:#0645ad}a::selection{background:rgba(255,255,0,.3);color:#0645ad}p{margin:1em 0}img{max-width:100%}h1,h2,h3,h4,h5,h6{color:#111;line-height:125%;margin-top:2em;font-weight:400}h4,h5,h6{font-weight:700}h1{font-size:2.5em}h2{font-size:2em}h3{font-size:1.5em}h4{font-size:1.2em}h5{font-size:1em}h6{font-size:.9em}blockquote{color:#666;margin:0;padding-left:3em;border-left:.5em #eee solid}hr{display:block;height:2px;border:0;border-top:1px solid #aaa;border-bottom:1px solid #eee;margin:1em 0;padding:0}pre,code,kbd,samp{color:#000;font-family:monospace,monospace;_font-family:'courier new',monospace;font-size:.98em}pre{white-space:pre;white-space:pre-wrap;word-wrap:break-word}b,strong{font-weight:700}dfn{font-style:italic}ins{background:#ff9;color:#000;text-decoration:none}mark{background:#ff0;color:#000;font-style:italic;font-weight:700}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}ul,ol{margin:1em 0;padding:0 0 0 2em}li p:last-child{margin-bottom:0}ul ul,ol ol{margin:.3em 0}dl{margin-bottom:1em}dt{font-weight:700;margin-bottom:.8em}dd{margin:0 0 .8em 2em}dd:last-child{margin-bottom:0}img{border:0;-ms-interpolation-mode:bicubic;vertical-align:middle}figure{display:block;text-align:center;margin:1em 0}figure img{border:0;margin:0 auto}figcaption{font-size:.8em;font-style:italic;margin:0 0 .8em}table{margin-bottom:2em;border-bottom:1px solid #ddd;border-right:1px solid #ddd;border-spacing:0;border-collapse:collapse}table th{padding:.2em 1em;background-color:#eee;border-top:1px solid #ddd;border-left:1px solid #ddd}table td{padding:.2em 1em;border-top:1px solid #ddd;border-left:1px solid #ddd;vertical-align:top}.author{font-size:1.2em;text-align:center}@media only screen and (min-width:480px){body{font-size:14px}}@media only screen and (min-width:768px){body{font-size:16px}}@media print{*{background:transparent!important;color:#000!important;filter:none!important;-ms-filter:none!important}body{font-size:12pt;max-width:100%}a,a:visited{text-decoration:underline}hr{height:1px;border:0;border-bottom:1px solid #000}a[href]:after{content:" (" attr(href) ")"}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;padding-right:1em;page-break-inside:avoid}tr,img{page-break-inside:avoid}img{max-width:100%!important}@page:left{margin:15mm 20mm 15mm 10mm}@page:right{margin:15mm 10mm 15mm 20mm}p,h2,h3{orphans:3;widows:3}h2,h3{page-break-after:avoid}}</style>
  <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
  <![endif]-->
</head>
<body>
<table>
<tbody>
<tr class="odd">
<td>Document number</td>
<td>D1819R0</td>
</tr>
<tr class="even">
<td>Date</td>
<td>2019-07-16</td>
</tr>
<tr class="odd">
<td>Reply-to</td>
<td>Vittorio Romeo &lt;<a href="mailto:vittorio.romeo@outlook.com" class="email">vittorio.romeo@outlook.com</a>&gt;</td>
</tr>
<tr class="even">
<td>Audience</td>
<td>Evolution Working Group Incubator (EWGI)</td>
</tr>
<tr class="odd">
<td>Project</td>
<td>ISO JTC1/SC22/WG21: Programming Language C++</td>
</tr>
</tbody>
</table>
<h1 id="interpolated-literals">Interpolated literals</h1>
<style>
.inline-link
{
    font-size: small;
    margin-top: -2.8em;
    margin-right: 4px;
    text-align: right;
    font-weight: bold;
}

code
{
    font-family: "Fira Code", monospace !important;
    font-size: 0.87em;
}

.sourceCode
{
    font-size: 0.95em;
}

a code
{
    color: #0645ad;
}
</style>
<h2 id="abstract">Abstract</h2>
<p>This paper proposes the addition of <em>interpolated literals</em> to the C++ language, a new form of string literals that can contain and retain arbitrary expressions. Interpolated literals provide a terse and readable syntax to generate strings embedding results of C++ expressions in a human-readable manner. The proposed feature is completely decoupled from any existing Standard Library facility, and provides a customization point to allow both Standard Library and user-defined types to visit the elements of an interpolated literal.</p>
<h2 id="overview">Overview</h2>
<p>One of the most common operations in any C++ code base is printing the value of a variable or the result of an expression alongside a human-readable message. Older techniques to achieve that goal, such as <code>&lt;iostream&gt;</code> and <code>printf</code>, have major drawbacks including <strong>verbosity</strong> and <strong>lack of type-safety</strong>. The upcoming <code>&lt;format&gt;</code> header addresses some of those problems, but still requires the user to pass the expressions that need to be printed separately from the body of the literal<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>, and the formatting happens at run-time rather than compile-time.</p>
<p>This paper proposes a flexible <em>language</em> feature that does not intend to compete with <code>&lt;format&gt;</code> - instead, it intends to complete the set of C++ formatting tools available for users by providing a compile-time-friendly mechanism to process expressions embedded in string literal.</p>
<table>
<colgroup>
<col style="width: 35%"></col>
<col style="width: 20%"></col>
<col style="width: 18%"></col>
<col style="width: 25%"></col>
</colgroup>
<tbody>
<tr class="odd">
<td></td>
<td><strong>Execution</strong></td>
<td><strong>i18n</strong></td>
<td><strong>Expressions</strong></td>
</tr>
<tr class="even">
<td><strong><code>&lt;format&gt;</code></strong></td>
<td>Run-time</td>
<td>Supported</td>
<td>Passed as arguments</td>
</tr>
<tr class="odd">
<td><strong>Interpolated literals</strong></td>
<td>Compile-time</td>
<td>Not supported</td>
<td>Embedded in literal</td>
</tr>
</tbody>
</table>
<h2 id="in-a-nutshell">In A Nutshell</h2>
<p>Here’s how interpolated literals can be used to print a variable to <code>stdout</code>:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb1-1" title="1"><span class="dt">int</span> port = <span class="dv">27015</span>;</a>
<a class="sourceLine" id="cb1-2" title="2"><span class="bu">std::</span>cout &lt;&lt; f<span class="st">&quot;Connecting on port {port}...</span><span class="sc">\n</span><span class="st">&quot;</span>;</a>
<a class="sourceLine" id="cb1-3" title="3"><span class="co">//            ^~~~~~~~~~~~~~~~~~~ ^~~~~ ^~~</span></a>
<a class="sourceLine" id="cb1-4" title="4"><span class="co">//            literal piece       |     |</span></a>
<a class="sourceLine" id="cb1-5" title="5"><span class="co">//                                expression piece</span></a>
<a class="sourceLine" id="cb1-6" title="6"><span class="co">//                                      |</span></a>
<a class="sourceLine" id="cb1-7" title="7"><span class="co">//                                      literal piece</span></a></code></pre></div>
<p>Arbitrary expressions are supported as part of the interpolated literal:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb2-1" title="1"><span class="dt">int</span> get_result() { <span class="cf">return</span> <span class="dv">42</span>; }</a>
<a class="sourceLine" id="cb2-2" title="2"><span class="bu">std::</span>cout &lt;&lt; f<span class="st">&quot;The result is {get_result()}</span><span class="sc">\n</span><span class="st">&quot;</span>;</a></code></pre></div>
<h3 id="syntax">Syntax</h3>
<ul>
<li><p>An interpolated literal always begins with the token <code>f</code>;</p></li>
<li><p>Expressions can be embedded inside the literal by surrounding them with curly braces;</p></li>
<li><p>Curly braces can be escaped by doubling them.</p></li>
</ul>
<h3 id="semantics">Semantics</h3>
<ul>
<li><p>An <em>interpolated literal expression</em> generates an <strong>anonymous type</strong> that satisfies the <code>InterpolatedLiteral</code> concept;</p></li>
<li><p>The anonymous type is roughly equivalent to a <em>lambda expression</em> that captures all the embedded expressions by reference, and accepts a callable which is invoked with all the parts of the interpolated literal;</p></li>
<li><p>The interpolated literal itself does nothing - it must be “consumed” by invoking it with a callable that performs a useful side effect (e.g. printing);</p></li>
<li><p>An interpolated literal is purely syntactic sugar for the definition of an anonymous callable, similarly to lambda expressions.</p></li>
</ul>
<h4 id="examples">Examples</h4>
<ul>
<li><p>Desugaring an interpolated literal to a lambda expression (#0):</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb3-1" title="1">f<span class="st">&quot;Connecting on port {port}...</span><span class="sc">\n</span><span class="st">&quot;</span></a>
<a class="sourceLine" id="cb3-2" title="2"><span class="co">// ...is roughly equivalent to...</span></a>
<a class="sourceLine" id="cb3-3" title="3">[&amp;](<span class="kw">auto</span>&amp;&amp; f) -&gt; <span class="kw">decltype</span>(<span class="kw">auto</span>)</a>
<a class="sourceLine" id="cb3-4" title="4">{</a>
<a class="sourceLine" id="cb3-5" title="5">    <span class="cf">return</span> f(<span class="st">&quot;Connecting on port &quot;</span>, port, <span class="st">&quot;...</span><span class="sc">\n</span><span class="st">&quot;</span>);</a>
<a class="sourceLine" id="cb3-6" title="6">}</a></code></pre></div>
<ul>
<li>Note that the <code>decltype(auto)</code> trailing return type is essential to retain the value category of <code>f</code>’s return value.</li>
</ul></li>
<li><p>Desugaring an interpolated literal to a lambda expression (#1):</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb4-1" title="1">f<span class="st">&quot;The result is {get_result()}</span><span class="sc">\n</span><span class="st">&quot;</span>;</a>
<a class="sourceLine" id="cb4-2" title="2"><span class="co">// ...is roughly equivalent to...</span></a>
<a class="sourceLine" id="cb4-3" title="3">[&amp;](<span class="kw">auto</span>&amp;&amp; f) -&gt; <span class="kw">decltype</span>(<span class="kw">auto</span>)</a>
<a class="sourceLine" id="cb4-4" title="4">{</a>
<a class="sourceLine" id="cb4-5" title="5">    <span class="cf">return</span> f(<span class="st">&quot;The result is &quot;</span>, get_result(), <span class="st">&quot;</span><span class="sc">\n</span><span class="st">&quot;</span>);</a>
<a class="sourceLine" id="cb4-6" title="6">}</a></code></pre></div></li>
</ul>
<h3 id="consuming-an-interpolated-literal">Consuming an interpolated literal</h3>
<p>Consumers of interpolated literals (e.g. <code>std::ostream</code>) can provide operator overloads accepting any object that satisfies <code>InterpolatedLiteral</code>. Note that the language feature is completely decoupled from any existing Standard Library facility. It is possible to use it with <code>&lt;iostream&gt;</code>, <code>&lt;string&gt;</code>, or any other user-defined type by simply defining appropriate overloads accepting <code>InterpolatedLiteral</code>.</p>
<h4 id="examples-1">Examples</h4>
<ul>
<li><p>Streaming an interpolated literal to <code>std::ostream</code>:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb5-1" title="1"><span class="bu">std::</span>ostream&amp; <span class="kw">operator</span>&lt;&lt;(<span class="bu">std::</span>ostream&amp; os, <span class="at">const</span> InterpolatedLiteral <span class="kw">auto</span>&amp; il)</a>
<a class="sourceLine" id="cb5-2" title="2">{</a>
<a class="sourceLine" id="cb5-3" title="3">    <span class="cf">return</span> il([&amp;](<span class="at">const</span> <span class="kw">auto</span>&amp;... xs)</a>
<a class="sourceLine" id="cb5-4" title="4">    {</a>
<a class="sourceLine" id="cb5-5" title="5">        <span class="cf">return</span> (os &lt;&lt; ... &lt;&lt; xs);</a>
<a class="sourceLine" id="cb5-6" title="6">    });</a>
<a class="sourceLine" id="cb5-7" title="7">}</a></code></pre></div></li>
<li><p>Appending an interpolated literal to <code>std::string</code>:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb6-1" title="1"><span class="bu">std::</span>string&amp; <span class="kw">operator</span>+=(<span class="bu">std::</span>string&amp; s, <span class="at">const</span> InterpolatedLiteral <span class="kw">auto</span>&amp; il)</a>
<a class="sourceLine" id="cb6-2" title="2">{</a>
<a class="sourceLine" id="cb6-3" title="3">    <span class="cf">return</span> il([&amp;](<span class="at">const</span> <span class="kw">auto</span>&amp;... xs)</a>
<a class="sourceLine" id="cb6-4" title="4">    {</a>
<a class="sourceLine" id="cb6-5" title="5">        <span class="cf">return</span> (s += ... += xs);</a>
<a class="sourceLine" id="cb6-6" title="6">    });</a>
<a class="sourceLine" id="cb6-7" title="7">}</a></code></pre></div></li>
</ul>
<h2 id="motivating-examples">Motivating examples</h2>
<p>The main purpose of this feature is to allow users to conveniently print human-readable strings containing expressions.</p>
<h3 id="debugging-and-logging">Debugging and logging</h3>
<p>Interpolated literals are the easiest and tersest way to provide logs or debug print statements.</p>
<ul>
<li><p>Logging via macro with stream-like syntax:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb7-1" title="1"><span class="dt">void</span> connect_to_host(net::ip ip, net::port port)</a>
<a class="sourceLine" id="cb7-2" title="2">{</a>
<a class="sourceLine" id="cb7-3" title="3">    LOG_TRACE &lt;&lt; f<span class="st">&quot;Connecting to {ip}:{port}...&quot;</span>;</a>
<a class="sourceLine" id="cb7-4" title="4">    <span class="co">// ...</span></a>
<a class="sourceLine" id="cb7-5" title="5">}</a></code></pre></div></li>
<li><p>Printing debug information via <code>std::ostream</code>:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb8-1" title="1"><span class="dt">int</span> main(<span class="dt">int</span> argc, <span class="dt">char</span>** argv)</a>
<a class="sourceLine" id="cb8-2" title="2">{</a>
<a class="sourceLine" id="cb8-3" title="3">    <span class="ot">assert</span>(argv == <span class="dv">3</span>);</a>
<a class="sourceLine" id="cb8-4" title="4">    <span class="bu">std::</span>cout &lt;&lt; f<span class="st">&quot;Input: {argv[1]}, Flags {argv[2]}</span><span class="sc">\n</span><span class="st">&quot;</span>;</a>
<a class="sourceLine" id="cb8-5" title="5">}</a></code></pre></div></li>
<li><p>Logging via function:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb9-1" title="1"><span class="cf">if</span> (<span class="at">const</span> <span class="kw">auto</span> [k, v] item = get_item(); !item.is_valid())</a>
<a class="sourceLine" id="cb9-2" title="2">{</a>
<a class="sourceLine" id="cb9-3" title="3">    log::error(f<span class="st">&quot;The item with key {k} and value {v} is not valid.&quot;</span>);</a>
<a class="sourceLine" id="cb9-4" title="4">    <span class="bu">std::</span>terminate();</a>
<a class="sourceLine" id="cb9-5" title="5">}</a></code></pre></div></li>
<li><p>Passing an interpolated literal to another function to provide debug-only printing:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb10-1" title="1"><span class="dt">void</span> debug_print([[<span class="at">maybe_unused</span>]] <span class="at">const</span> InterpolatedLiteral <span class="kw">auto</span>&amp; il)</a>
<a class="sourceLine" id="cb10-2" title="2">{</a>
<a class="sourceLine" id="cb10-3" title="3"><span class="pp">#ifndef NDEBUG</span></a>
<a class="sourceLine" id="cb10-4" title="4">    <span class="bu">std::</span>cerr &lt;&lt; il;</a>
<a class="sourceLine" id="cb10-5" title="5"><span class="pp">#endif</span></a>
<a class="sourceLine" id="cb10-6" title="6">}</a>
<a class="sourceLine" id="cb10-7" title="7"></a>
<a class="sourceLine" id="cb10-8" title="8">[[<span class="at">nodiscard</span>]] <span class="dt">long</span> factorial(<span class="dt">long</span> n)</a>
<a class="sourceLine" id="cb10-9" title="9">{</a>
<a class="sourceLine" id="cb10-10" title="10">    debug_print(f<span class="st">&quot;factorial({n})&quot;</span>)</a>
<a class="sourceLine" id="cb10-11" title="11"></a>
<a class="sourceLine" id="cb10-12" title="12">    <span class="cf">if</span> (n &lt; <span class="dv">1</span>) { <span class="cf">return</span> <span class="dv">1</span>; }</a>
<a class="sourceLine" id="cb10-13" title="13">    <span class="cf">return</span> n * factorial(n - <span class="dv">1</span>);</a>
<a class="sourceLine" id="cb10-14" title="14">}</a></code></pre></div></li>
</ul>
<h3 id="automated-coloring">Automated coloring</h3>
<p>If the feature is defined to automatically tag arguments as either <em>string literal pieces</em> or <em>embedded expressions</em>, it is possible to automatically color the embedded expressions when printing to a console. This can also be achieved by inspecting the type of the arguments passed to the higher-order function, at the cost of not being able to differentiate between string literal pieces and embedded expressions that evaluate to string literals. E.g.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb11-1" title="1"><span class="kw">struct</span> pretty_cout { <span class="co">/* ... */</span> };</a>
<a class="sourceLine" id="cb11-2" title="2"></a>
<a class="sourceLine" id="cb11-3" title="3">pretty_cout&amp; <span class="kw">operator</span>+=(pretty_cout&amp; os, <span class="at">const</span> InterpolatedLiteral <span class="kw">auto</span>&amp; il)</a>
<a class="sourceLine" id="cb11-4" title="4">{</a>
<a class="sourceLine" id="cb11-5" title="5">    il([&amp;os](<span class="at">const</span> <span class="kw">auto</span>&amp;... xs)</a>
<a class="sourceLine" id="cb11-6" title="6">    {</a>
<a class="sourceLine" id="cb11-7" title="7">        ([&amp;](<span class="at">const</span> <span class="kw">auto</span>&amp; x)</a>
<a class="sourceLine" id="cb11-8" title="8">        {</a>
<a class="sourceLine" id="cb11-9" title="9">            <span class="cf">if</span> <span class="kw">constexpr</span> (is_string_literal(x))</a>
<a class="sourceLine" id="cb11-10" title="10">            {</a>
<a class="sourceLine" id="cb11-11" title="11">                os &lt;&lt; x;</a>
<a class="sourceLine" id="cb11-12" title="12">            }</a>
<a class="sourceLine" id="cb11-13" title="13">            <span class="cf">else</span> <span class="cf">if</span> <span class="kw">constexpr</span> (is_embedded_expression(x))</a>
<a class="sourceLine" id="cb11-14" title="14">            {</a>
<a class="sourceLine" id="cb11-15" title="15">                os &lt;&lt; ansi_color::green &lt;&lt; x &lt;&lt; ansi_color::white;</a>
<a class="sourceLine" id="cb11-16" title="16">            }</a>
<a class="sourceLine" id="cb11-17" title="17">        }(xs), ...);</a>
<a class="sourceLine" id="cb11-18" title="18">    });</a>
<a class="sourceLine" id="cb11-19" title="19"></a>
<a class="sourceLine" id="cb11-20" title="20">    <span class="cf">return</span> os;</a>
<a class="sourceLine" id="cb11-21" title="21">}</a>
<a class="sourceLine" id="cb11-22" title="22"></a>
<a class="sourceLine" id="cb11-23" title="23"><span class="kw">constexpr</span> <span class="bu">std::</span>string_view name{<span class="st">&quot;Bob&quot;</span>};</a>
<a class="sourceLine" id="cb11-24" title="24">pretty_cout{} &lt;&lt; f<span class="st">&quot;Hello world! My name is {name}!</span><span class="sc">\n</span><span class="st">&quot;</span>;</a>
<a class="sourceLine" id="cb11-25" title="25">    <span class="co">// &quot;Bob&quot; gets automatically colored in green in the console output.</span></a></code></pre></div>
<h2 id="faq">FAQ</h2>
<ul>
<li><p>Why a callable object? Why not just capture things into a <code>std::tuple</code>?</p>
<ul>
<li>Capturing this into a <code>std::tuple</code> introduces an undesirable dependency between the language feature and <code>&lt;tuple&gt;</code>. Furthermore, constructing a tuple from a function call is trivial (e.g. <code>il([](const auto&amp;... xs){ return std::tuple{xs...}; })</code>), while expanding a tuple requires expensive compile-time machinery such as <code>std::apply</code>.</li>
</ul></li>
<li><p>Why a language feature? Can’t we just wait for reflection?</p>
<ul>
<li>This feature doesn’t only require reflection, but also code synthesis. With reflection, we would need to be able to figure extract the variable names from the literals, then look them up, and then synthesize a lambda expression. This is not covered by the current Reflection TS and might take considerable time.</li>
</ul></li>
<li><p>Why not wait until <code>&lt;format&gt;</code> get <code>constexpr</code> formatting capabilities?</p>
<ul>
<li>Even a fully-<code>constexpr</code> <code>&lt;format&gt;</code> library does not match the user-friendliness and intuitiveness of interpolated literals. Expressions cannot be embedded as part of the string literal unless we get extremely advanced reflection and code synthesis facilities (see above), and the formatting will be deeply tied to a particular library feature. The proposed language in this paper completely sidesteps these issues (and potential implementation complexities or compilation time explosion) by making interpolated literals a first-class feature. Note that this proposal does not compete with <code>&lt;format&gt;</code> - the <code>&lt;format&gt;</code> header is a great candidate for a consumer of interpolated literals.</li>
</ul></li>
</ul>
<h2 id="todo">TODO</h2>
<p>This proposal is just a draft to see EWGI likes the approach. If there is enough positive feedback, these are some points to think about:</p>
<ul>
<li><p>Tagging embedded expressions;</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb12-1" title="1">f<span class="st">&quot;My name is {name}&quot;</span></a>
<a class="sourceLine" id="cb12-2" title="2"><span class="co">// ...could expand to...</span></a>
<a class="sourceLine" id="cb12-3" title="3">[&amp;](<span class="kw">auto</span>&amp;&amp; f) -&gt; <span class="kw">decltype</span>(<span class="kw">auto</span>)</a>
<a class="sourceLine" id="cb12-4" title="4">{</a>
<a class="sourceLine" id="cb12-5" title="5">    <span class="cf">return</span> f(literal{<span class="st">&quot;My name is &quot;</span>}, expression{name});</a>
<a class="sourceLine" id="cb12-6" title="6">}</a></code></pre></div></li>
<li><p>Dangling references concerns, maybe a syntax to capture rvalues by move;</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb13-1" title="1">f<span class="st">&quot;My name is {name}&quot;</span></a>
<a class="sourceLine" id="cb13-2" title="2">    <span class="co">// Captures `name` by reference.</span></a>
<a class="sourceLine" id="cb13-3" title="3"></a>
<a class="sourceLine" id="cb13-4" title="4">f[=]<span class="st">&quot;My name is {name}&quot;</span></a>
<a class="sourceLine" id="cb13-5" title="5">    <span class="co">// Captures `name` by value, fully supports lambda syntax.</span></a></code></pre></div></li>
<li><p>Nested interpolated literals, passing string literals in curly braces;</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb14-1" title="1">f<span class="st">&quot;Hello {</span><span class="sc">\&quot;</span><span class="st">world</span><span class="sc">\&quot;</span><span class="st">}&quot;</span></a>
<a class="sourceLine" id="cb14-2" title="2">    <span class="co">// Should probably work and evaluate to &quot;Hello world&quot;.</span></a>
<a class="sourceLine" id="cb14-3" title="3"></a>
<a class="sourceLine" id="cb14-4" title="4">f<span class="st">&quot;Hello {f</span><span class="sc">\&quot;</span><span class="st">world</span><span class="sc">\&quot;</span><span class="st">(stringify)}&quot;</span></a>
<a class="sourceLine" id="cb14-5" title="5">    <span class="co">// Ditto.</span></a></code></pre></div></li>
<li><p>Formatting embedded expressions, for example floating point digits;</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode cpp"><code class="sourceCode cpp"><a class="sourceLine" id="cb15-1" title="1"><span class="at">const</span> <span class="dt">double</span> value = <span class="fl">12.34567</span>;</a>
<a class="sourceLine" id="cb15-2" title="2"><span class="at">const</span> <span class="dt">int</span> width = <span class="dv">10</span>;</a>
<a class="sourceLine" id="cb15-3" title="3"><span class="at">const</span> <span class="dt">int</span> precision = <span class="dv">4</span>;</a>
<a class="sourceLine" id="cb15-4" title="4"></a>
<a class="sourceLine" id="cb15-5" title="5">f<span class="st">&quot;The result is {value:{width}.{precision}}&quot;</span></a>
<a class="sourceLine" id="cb15-6" title="6">    <span class="co">// Might provide information to evaluate result to &quot;12.34&quot;.</span></a>
<a class="sourceLine" id="cb15-7" title="7"></a>
<a class="sourceLine" id="cb15-8" title="8"><span class="co">// ...could expand to...</span></a>
<a class="sourceLine" id="cb15-9" title="9"></a>
<a class="sourceLine" id="cb15-10" title="10">[&amp;](<span class="kw">auto</span>&amp;&amp; f) -&gt; <span class="kw">decltype</span>(<span class="kw">auto</span>)</a>
<a class="sourceLine" id="cb15-11" title="11">{</a>
<a class="sourceLine" id="cb15-12" title="12">    <span class="cf">return</span> f(literal{<span class="st">&quot;The result is &quot;</span>},</a>
<a class="sourceLine" id="cb15-13" title="13">             decimal_expression{value, width, precision});</a>
<a class="sourceLine" id="cb15-14" title="14">}</a></code></pre></div></li>
<li><p>Standard Library support;</p>
<ul>
<li>In the case of tagged elements, an <code>&lt;interpolated_literal&gt;</code> header might be provided by the Standard Library to expose the tag types.</li>
</ul></li>
<li><p>Use cases in the Standard Library (<code>&lt;iostream&gt;</code>, <code>&lt;string&gt;</code>, <code>&lt;format&gt;</code>);</p></li>
<li><p>Think about unconventional use cases.</p></li>
</ul>
<section class="footnotes">
<hr />
<ol>
<li id="fn1"><p>I.e. the expressions to format cannot be embedded directly in the literal. <code>{}</code> placeholders must instead be used, and the expressions have to be passed as arguments to the <code>format</code> call separately. See the <a href="#FAQ">FAQ</a> section for more details.<a href="#fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>
</body>
</html>
