<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3011: Requirements for assert(E) inconsistent with C</title>
<meta property="og:title" content="Issue 3011: Requirements for assert(E) inconsistent with C">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3011.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="3011"><a href="lwg-active.html#3011">3011</a>. Requirements for <code>assert(<em>E</em>)</code> inconsistent with C</h3>
<p><b>Section:</b> 19.3 <a href="https://wg21.link/assertions">[assertions]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2017-08-18 <b>Last modified:</b> 2018-08-20</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#assertions">issues</a> in [assertions].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The C standard says that the expression in an <code>assert</code> must have a scalar type, and implies (or at least allows) 
that the condition is tested by comparison to zero. C++ says that the expression is a constant subexpression if it can 
be contextually converted to <code>bool</code>. Those ways to test the condition are not equivalent.
<p/>
It's possible to have expressions that meet the C++ requirements for a constant subexpression, but fail to meet the C 
requirements, and so don't compile.
</p>
<blockquote><pre>
#include &lt;stdlib.h&gt;

// A toy implementation of assert:
#define assert(E) (void)(((E) != 0) || (abort(), 0))

struct X {
  constexpr explicit operator bool() const { return true; }
};

constexpr bool f(const X&amp; x) {
  assert(x);
  return true;
}
</pre></blockquote>
<p>
C++ says that <code>assert(x)</code> is a constant subexpression, but as it doesn't have scalar type it's not even a valid expression.
<p/>
I think either 19.3.2 <a href="https://wg21.link/cassert.syn">[cassert.syn]</a> or 19.3.3 <a href="https://wg21.link/assertions.assert">[assertions.assert]</a> should repeat the requirement from C that 
<em>E</em> has scalar type, either normatively or in a note. We should also consider whether "contextually converted to <code>bool</code>" 
is the right condition, or if we should use comparison to zero instead.
</p>

<p><i>[2017-11 Albuquerque Wednesday night issues processing]</i></p>

<p>Priority set to 2; status to Open</p>
<p>Jonathan is discussing this with WG14</p>

<p><i>[2018-08-20, Jonathan comments]</i></p>

<p>
This was reported to WG14 as <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2207.htm">N2207</a>.
</p>


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




</body>
</html>
