6

Our project uses C++11/14, and we want to use nullptr instead of 0 or NULL with pointers, even when 0 (as an integer literal) is allowed.

I have the following code:

int main()
{
    int *ptr1 = nullptr; // #1
    int *ptr2 = 0;       // #2
}

If I compile with GCC (5.3.0) and the flag -Wzero-as-null-pointer-constant it warnings in #2, but I can't find a similar flag in Clang. If I compile the code with Clang (3.7.1) and the flag -Weverything, I don't get any warning about #2.

So, is there any way to get a similar warning for this in Clang?

101010
  • 41,839
  • 11
  • 94
  • 168
Daniel
  • 2,657
  • 1
  • 17
  • 22
  • 1
    It's even worse than that. In C++ you're allowed to use any integer constant expression evaluating to zero, not just a literal... e.g. `!!!!!1`. – 6502 Jan 22 '16 at 18:31
  • 1
    @6502 I think that was the behavior in C++98, but C++14 seems to change that. See this [answer](http://stackoverflow.com/a/33656869/2484624). If I understand correctly, `!!!!!1` is an integral constant expression (that evaluates to `false`) and not an integer literal. – Daniel Jan 22 '16 at 18:50
  • IIRC clang actually comes with a tool to automatically replace old null pointer constants with `nullptr`. – T.C. Jan 22 '16 at 19:08

3 Answers3

8

clang has this warning as of 5.0; I added it here.

thakis
  • 5,405
  • 1
  • 33
  • 33
3

Clang doesn't support these kind of warnings (i.e., there's no -Wzero-as-null-pointer-constant equivalent in Clang). You can see it your self if you add -Weverything option (mind do it only for testing), which enables all Clang's warnings.

Live Demo

101010
  • 41,839
  • 11
  • 94
  • 168
1

Use -Wzero-as-null-pointer-constant.

  • Available at least since clang10 (around 2017).
  • Same as gcc flag.

so bad it is not enabled by default by -Wall -Wextra...

Steve
  • 21
  • 4