<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2950: std::byte operations are misspecified</title>
<meta property="og:title" content="Issue 2950: std::byte operations are misspecified">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2950.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#C++20">C++20</a> status.</em></p>
<h3 id="2950"><a href="lwg-defects.html#2950">2950</a>. <code>std::byte</code> operations are misspecified</h3>
<p><b>Section:</b> 17.2.5 <a href="https://wg21.link/support.types.byteops">[support.types.byteops]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Thomas K&ouml;ppe <b>Opened:</b> 2017-03-24 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>1
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The operations for <code>std::byte</code> (17.2.5 <a href="https://wg21.link/support.types.byteops">[support.types.byteops]</a>) are currently specified to have undefined 
behaviour in general cases, since the type of the expression <i>expr</i> that appears in <code>return byte(<i>expr</i>)</code> is 
obtained by the arithmetic conversion rules and has higher conversion rank than <code>unsigned char</code>. Therefore, the 
value of the expression may be outside the range of the enum (for example, consider <code>~0</code>), and by 
7.6.1.9 <a href="https://wg21.link/expr.static.cast">[expr.static.cast]</a> p10 the conversion results in undefined behaviour.
<p/>
I believe the original intent of the specification could be expressed correctly with the following, more verbose sequence 
of casts. I will only give one representative example:
</p>
<blockquote>
<pre>
byte operator&lt;&lt;(byte b, IntType shift)
</pre>
<blockquote>
<p>
Equivalent to: <code>return byte(static_cast&lt;unsigned char&gt;(static_cast&lt;unsigned char&gt;(b) &lt;&lt; shift));</code>
</p>
</blockquote>
</blockquote>

<p><i>[
2017-06-27 P1 after 5 positive votes on c++std-lib.
]</i></p>


<p><i>[2017-06-28, STL comments and provides wording]</i></p>

<p>
This proposed resolution performs its work in <code>unsigned int</code>, which is immune to promotion. For <code>op=</code>, 
I'm avoiding unnecessary verbosity.
<p/>
It stylistically uses <code>static_cast</code>s instead of functional-style casts. All of the <code>static_cast</code>s are 
intentional, although not all of them are strictly necessary. I felt that it was simpler to always follow the 
same pattern for type conversions, instead of skipping <code>static_cast</code>s by taking advantage of the possible 
ranges of values. (I could prepare an alternative PR to avoid unnecessary casts.) I'm not <code>static_cast</code>ing 
the shift arguments, because of how 7.6.7 <a href="https://wg21.link/expr.shift">[expr.shift]</a> works.
<p/>
For <code>to_integer()</code>, there's a tiny question. According to 7.6.1.9 <a href="https://wg21.link/expr.static.cast">[expr.static.cast]</a>/9, 
<code>static_cast</code>ing from <code>[128, 255]</code> bytes to <code>signed</code> (behavior) <code>char</code>s triggers unspecified behavior, 
whereas using <code>unsigned char</code> as an intermediate type would produce implementation-defined behavior, 
7.3.9 <a href="https://wg21.link/conv.integral">[conv.integral]</a>/3. This question is basically theoretical, and it's unaffected by this proposed resolution 
(which is just changing a functional-style cast to a <code>static_cast</code>).
</p>

<p><i>[2016-07, Toronto Thursday night issues processing]</i></p>

<p>Status to Ready</p>


<p id="res-2950"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4659">N4659</a>.
</p>

<ol>
<li><p>Edit 17.2.5 <a href="https://wg21.link/support.types.byteops">[support.types.byteops]</a> as indicated:</p>

<blockquote>
<pre>
template &lt;class IntType&gt;
  constexpr byte&amp; operator&lt;&lt;=(byte&amp; b, IntType shift) noexcept;
</pre>
<blockquote>
<p>
-1- <i>Remarks:</i> This function shall not participate in overload resolution unless <code>is_integral_v&lt;IntType&gt;</code> 
is <code>true</code>.
<p/>
-2- <i>Effects:</i> Equivalent to: <code>return b = <ins>b &lt;&lt; shift</ins><del>byte(static_cast&lt;unsigned char&gt;(b) &lt;&lt; shift)</del>;</code>
</p>
</blockquote>
<pre>
template &lt;class IntType&gt;
  constexpr byte operator&lt;&lt;(byte b, IntType shift) noexcept;
</pre>
<blockquote>
<p>
-3- <i>Remarks:</i> This function shall not participate in overload resolution unless <code>is_integral_v&lt;IntType&gt;</code> is
<code>true</code>.
<p/>
-4- <i>Effects:</i> Equivalent to: <code>return 
<ins>static_cast&lt;byte&gt;(static_cast&lt;unsigned char&gt;(static_cast&lt;unsigned int&gt;(b) &lt;&lt; 
shift))</ins><del>byte(static_cast&lt;unsigned char&gt;(b) &lt;&lt; shift)</del>;</code>
</p>
</blockquote>
<pre>
template &lt;class IntType&gt;
  constexpr byte&amp; operator&gt;&gt;=(byte&amp; b, IntType shift) noexcept;
</pre>
<blockquote>
<p>
-5- <i>Remarks:</i> This function shall not participate in overload resolution unless <code>is_integral_v&lt;IntType&gt;</code> is
<code>true</code>.
<p/>
-6- <i>Effects:</i> Equivalent to: <code>return b = <ins>b &gt;&gt; shift</ins><del>byte(static_cast&lt;unsigned char&gt;(b) &gt;&gt; shift)</del>;</code>
</p>
</blockquote>
<pre>
template &lt;class IntType&gt;
  constexpr byte operator&gt;&gt;(byte b, IntType shift) noexcept;
</pre>
<blockquote>
<p>
-7- <i>Remarks:</i> This function shall not participate in overload resolution unless <code>is_integral_v&lt;IntType&gt;</code> is
<code>true</code>.
<p/>
-8- <i>Effects:</i> Equivalent to: <code>return 
<ins>static_cast&lt;byte&gt;(static_cast&lt;unsigned char&gt;(static_cast&lt;unsigned int&gt;(b) &gt;&gt; 
shift))</ins><del>byte(static_cast&lt;unsigned char&gt;(b) &gt;&gt; shift)</del>;</code>
</p>
</blockquote>
<pre>
constexpr byte&amp; operator|=(byte&amp; l, byte r) noexcept;
</pre>
<blockquote>
<p>
-9- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
return l = <ins>l | r</ins><del>byte(static_cast&lt;unsigned char&gt;(l) | static_cast&lt;unsigned char&gt;(r))</del>;
</pre></blockquote>
</blockquote>
<pre>
constexpr byte operator|(byte l, byte r) noexcept;
</pre>
<blockquote>
<p>
-10- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
return <ins>static_cast&lt;byte&gt;(static_cast&lt;unsigned char&gt;(static_cast&lt;unsigned int&gt;(l) | 
static_cast&lt;unsigned int&gt;(r)))</ins><del>byte(static_cast&lt;unsigned char&gt;(l) | 
static_cast&lt;unsigned char&gt;(r))</del>;
</pre></blockquote>
</blockquote>
<pre>
constexpr byte&amp; operator&amp;=(byte&amp; l, byte r) noexcept;
</pre>
<blockquote>
<p>
-11- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
return l = <ins>l &amp; r</ins><del>byte(static_cast&lt;unsigned char&gt;(l) &amp; static_cast&lt;unsigned char&gt;(r))</del>;
</pre></blockquote>
</blockquote>
<pre>
constexpr byte operator&amp;(byte l, byte r) noexcept;
</pre>
<blockquote>
<p>
-12- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
return <ins>static_cast&lt;byte&gt;(static_cast&lt;unsigned char&gt;(static_cast&lt;unsigned int&gt;(l) &amp; 
static_cast&lt;unsigned int&gt;(r)))</ins><del>byte(static_cast&lt;unsigned char&gt;(l) &amp; 
static_cast&lt;unsigned char&gt;(r))</del>;
</pre></blockquote>
</blockquote>
<pre>
constexpr byte&amp; operator^=(byte&amp; l, byte r) noexcept;
</pre>
<blockquote>
<p>
-13- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
return l = <ins>l ^ r</ins><del>byte(static_cast&lt;unsigned char&gt;(l) ^ static_cast&lt;unsigned char&gt;(r))</del>;
</pre></blockquote>
</blockquote>
<pre>
constexpr byte operator^(byte l, byte r) noexcept;
</pre>
<blockquote>
<p>
-14- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
return <ins>static_cast&lt;byte&gt;(static_cast&lt;unsigned char&gt;(static_cast&lt;unsigned int&gt;(l) ^ 
static_cast&lt;unsigned int&gt;(r)))</ins><del>byte(static_cast&lt;unsigned char&gt;(l) ^ 
static_cast&lt;unsigned char&gt;(r))</del>;
</pre></blockquote>
</blockquote>
<pre>
constexpr byte operator~(byte b) noexcept;
</pre>
<blockquote>
<p>
-15- <i>Effects:</i> Equivalent to: <code>return 
<ins>static_cast&lt;byte&gt;(static_cast&lt;unsigned char&gt;(~static_cast&lt;unsigned 
int&gt;(b)))</ins><del>byte(~static_cast&lt;unsigned char&gt;(b))</del>;</code>
</p>
</blockquote>
<pre>
template &lt;class IntType&gt;
  constexpr IntType to_integer(byte b) noexcept;
</pre>
<blockquote>
<p>
-16- <i>Remarks:</i> This function shall not participate in overload resolution unless <code>is_integral_v&lt;IntType&gt;</code> is
<code>true</code>.
<p/>
-17- <i>Effects:</i> Equivalent to: <code>return <ins>static_cast&lt;IntType&gt;</ins><del>IntType</del>(b);</code>
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
