[cfe-dev] Q. on pointer/integer comparison

Richard Smith richard at metafoo.co.uk
Wed Apr 2 18:58:08 PDT 2014


On Wed, Apr 2, 2014 at 5:47 PM, Robinson, Paul <
Paul_Robinson at playstation.sony.com> wrote:

> With -std=c++11, clang will complain about the expression in
> bar() but not foo().
>
>     bool foo(int *p) { return (p != 0); }
>     bool bar(int* p) { return (p != (1 - 1)); }
>
> With -std=c++03, clang accepts both.
>
> [expr.const]p3 has a Note that says an integral constant expression
> can be used as a null pointer constant.
> So, I think Clang should accept both even in C++11 mode.
>
> Bug, or my usual confusion about the standard?  (I can see issuing
> a warning, because it would be hard to contrive a real-world case
> where somebody would actually want to do this; but if it conforms,
> Clang should not reject it.)


This was changed by DR 903:

  http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#903

We implement this DR in C++11 onwards, since the C++98 rules are *really*
broken when combined with C++11's generalized constant expressions. Without
this change, ridiculous things like

  struct S { int n; };
  int *p = S().n;

... would compile, because S().n is an integral constant expression with
value 0.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140402/e24f11fe/attachment.html>


More information about the cfe-dev mailing list