Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat new capabilities do user-defined literals add to C++?
    primarykey
    data
    text
    <p><a href="http://en.wikipedia.org/wiki/C%2B%2B11" rel="noreferrer">C++11</a> introduces <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2378.pdf" rel="noreferrer">user-defined literals</a> which will allow the introduction of new literal syntax based on existing literals (<code>int</code>, <code>hex</code>, <code>string</code>, <code>float</code>) so that any type will be able to have a literal presentation.</p> <p><strong>Examples:</strong></p> <pre><code>// imaginary numbers std::complex&lt;long double&gt; operator "" _i(long double d) // cooked form { return std::complex&lt;long double&gt;(0, d); } auto val = 3.14_i; // val = complex&lt;long double&gt;(0, 3.14) // binary values int operator "" _B(const char*); // raw form int answer = 101010_B; // answer = 42 // std::string std::string operator "" _s(const char* str, size_t /*length*/) { return std::string(str); } auto hi = "hello"_s + " world"; // + works, "hello"_s is a string not a pointer // units assert(1_kg == 2.2_lb); // give or take 0.00462262 pounds </code></pre> <p>At first glance this looks very cool but I'm wondering how applicable it really is, when I tried to think of having the suffixes <code>_AD</code> and <code>_BC</code> create dates I found that it's problematic due to operator order. <code>1974/01/06_AD</code> would first evaluate <code>1974/01</code> (as plain <code>int</code>s) and only later the <code>06_AD</code> (to say nothing of August and September having to be written without the <code>0</code> for octal reasons). This can be worked around by having the syntax be <code>1974-1/6_AD</code> so that the operator evaluation order works but it's clunky. </p> <p>So what my question boils down to is this, do you feel this feature will justify itself? What other literals would you like to define that will make your C++ code more readable?</p> <hr> <p>Updated syntax to fit the final draft on June 2011</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload