<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2338: &sect;[re.traits]/7 expects of locale facets something not guaranteed by [locale.facet]/4</title>
<meta property="og:title" content="Issue 2338: &sect;[re.traits]/7 expects of locale facets something not guaranteed by [locale.facet]/4">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2338.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#Open">Open</a> status.</em></p>
<h3 id="2338"><a href="lwg-active.html#2338">2338</a>. &sect;[re.traits]/7 expects of locale facets something not guaranteed by [locale.facet]/4</h3>
<p><b>Section:</b> 28.6.6 <a href="https://wg21.link/re.traits">[re.traits]</a>, 28.3.3.1.2.2 <a href="https://wg21.link/locale.facet">[locale.facet]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Sergey Zubkov <b>Opened:</b> 2013-10-15 <b>Last modified:</b> 2025-03-06</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#re.traits">issues</a> in [re.traits].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
28.6.6 <a href="https://wg21.link/re.traits">[re.traits]</a>/7, begins with "if <code>typeid(use_facet&lt;collate&lt;charT&gt; &gt;) == typeid(collate_byname&lt;charT&gt;)</code>", 
which appears to be pseudocode with the intention to convey that the collate facet has not been replaced by the user. Cf. the wording in 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1429.htm">N1429</a> "there is no portable way to implement 
<code>transform_primary</code> in terms of <code>std::locale</code>, since even if the sort key format returned by 
<code>std::collate_byname&lt;&gt;::transform</code> is known and can be converted into a primary sort key, the user can still 
install their own custom <code>std::collate</code> implementation into the locale object used, and that can use any sort key 
format they see fit.".
<p/>
Taken literally, 28.6.6 <a href="https://wg21.link/re.traits">[re.traits]</a>/7 appears to imply that named locales are required to hold their collate facets with 
dynamic type <code>std::collate_byname&lt;charT&gt;</code>, which is in fact true in some implementations (e.g libc++), but not others 
(e.g. libstdc++). This does not follow from the description of <code>_byname</code> in 28.3.3.1.2.2 <a href="https://wg21.link/locale.facet">[locale.facet]</a>/4, which is only 
required to provide equivalent semantics, to the named locale's facet, not to actually be one.
</p>


<p><i>[2015-05-06 Lenexa: Move to Open]</i></p>


<p>MC, RP: Consequence of failing to follow the rule is UB.</p>
<p>MC: Tightening of requirements.</p>
<p>RP: It should be this way, we just didn't impose it before.</p>
<p>MC: Second change is a bug fix, original code didn't work.</p>
<p>TK: Doesn't seem to make things worse.</p>
<p>Bring up in larger group tomorrow.</p>
<p>JW arrives.</p>
<p>JW: libstdc++ violates this due to two std::string ABIs.</p>
<p>JW: This prevents installing a type derived from Facet_byname, constrains the implementor from using a smarter derived class version.</p>
<p>JW: Can't look at facet id to detect replacement, because replacements have the same id.</p>
<p>RP: Can you give it multiple ids through multiple inheritance?</p>
<p>JW: No, the facet mechanism wouldn't like that.</p>
<p>JW: We should also ask Martin Sebor, he's implemented this stuff recently.</p>
<p>MC: Sounds like this resolution doesn't work, need a better solution.</p>
<p>JW: Write in words "if the facet has not been replaced by the user", the implementation knows how to detect that, but not like this.</p>
<p>RP: User RE traits need to detect this too.</p>
<p>JW: =(</p>
<p>Move to Open, JW will invite Martin Sebor to join LWG for discussion.</p>
<p>Later ... </p>
<p>JW: This is not needed for user specializations after all.</p>
<p>MC: Agree, [re.traits]/7 only applies to the stdlib traits.</p>
<p>NM: Effects: doesn't make sense.</p>
<p>JW, NM, Martin Sebor to come up with new wording.</p>

<p><i>[2025-03-06; Jonathan comments]</i></p>

<p>
LWG <a href="lwg-defects.html#4186" title="regex_traits::transform_primary mistakenly detects typeid of a function (Status: WP)">4186</a><sup><a href="https://cplusplus.github.io/LWG/issue4186" title="Latest snapshot">(i)</a></sup> fixed the missing <code class='backtick'>(getloc())</code> part.
Maybe we can resolve this differently, by changing 28.6.6 <a href="https://wg21.link/re.traits">[re.traits]</a>
to:
<blockquote>
Let <code class='backtick'>C</code> be <code>use_facet&lt;collate&lt;charT&gt; &gt;(getloc())</code>.
If <code>typeid(C) == typeid(collate_byname&lt;charT&gt;)
|| typeid(C) == typeid(collate&lt;charT&gt;)</code> and the form of ...
</blockquote>
</p>



<p id="res-2338"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3691.</p>

<ol>
<li><p>Modify 28.3.3.1.2.2 <a href="https://wg21.link/locale.facet">[locale.facet]</a>/4 as indicated:</p>

<blockquote><p>
For some standard facets a standard "...<code>_byname</code>" class, derived from it, implements the virtual function
semantics <del>equivalent to</del><ins>provided by</ins> that facet of the locale constructed by <code>locale(const char*)</code> 
with the same name.
Each such facet provides a constructor that takes a <code>const char*</code> argument, which names the locale, and a
<code>refs</code> argument, which is passed to the base class constructor. Each such facet also provides a constructor
that takes a string argument <code>str</code> and a <code>refs</code> argument, which has the same effect as calling the first
constructor with the two arguments <code>str.c_str()</code> and <code>refs</code>. If there is no "...<code>_byname</code>" 
version of a facet, the base class implements named locale semantics itself by reference to other facets. <ins>For any 
locale <code>loc</code> constructed by <code>locale(const char*)</code> and facet <code>Facet</code> that has a corresponding standard 
<code>Facet_byname</code> class, <code>typeid(use_facet&lt;Facet&gt;(loc)) == typeid(Facet_byname)</code>.</ins>
</p></blockquote>
</li>

<li><p>Modify 28.6.6 <a href="https://wg21.link/re.traits">[re.traits]</a>/7 as indicated:</p>

<blockquote><pre>
template &lt;class ForwardIterator&gt;
  string_type transform_primary(ForwardIterator first, ForwardIterator last) const;
</pre><blockquote>
<p>
-7- <i>Effects:</i> if <code>typeid(use_facet&lt;collate&lt;charT&gt; &gt;<ins>(getloc())</ins>) == typeid(collate_byname&lt;charT&gt;)</code> 
and the form of the sort key returned by <code>collate_byname&lt;charT&gt;::transform(first, last)</code> is known and
can be converted into a primary sort key then returns that key, otherwise returns an empty string.
</p>
</blockquote></blockquote>
</li>

</ol>





</body>
</html>
