`type * var = (int) 0`, legal or not?

The following example:

char *var = (int)0;

Compiles to gcc and cl.exe, but causes an error in clang:

cannot initialize a variable of type 'char *' with rvalue of type 'int'

Who is right here?


What is it worth, C ++ 11 says (emphasis mine)

4.10 / 1

The null pointer constant is an integer expression const (5.19) prvalue of an integer type that evaluates to zero or a prdue of type std :: nullptr_t. The null pointer constant can be converted to a pointer type; the result is a null pointer value of this type and ...

and

5.19 / 3

. [: (8.3.4, 5.3.4) (9.6) , (7.2), (4.10), (7.6.2). - ]

, , . cl.exe ( ), gcc . , , gcc?

+4
1

, ++ 11, , :

(5.19) prvalue ,

, CWG 903, ++ 11, :

(2.13.2 [lex.icon]) 0

(int)0 - prvalue , , 0.

++ 11, ++ 11.

+6

Source: https://habr.com/ru/post/1670955/


All Articles