Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by blackpill0w on Monday May 15, 2023 @03:44PM
(#35953481)
>C doesn't have this problem
I don't think a strong type system is a `problem`, implicit conversions can lead to so many annoying and hard to find bugs.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by ori_b on Monday May 15, 2023 @03:46PM
(#35953510)
That's a fine general sentiment. However, in this context it's a problem if you want to assign NULL to a pointer without a cast, which is why C++ added the magically convertible nullptr to add to the the magically convertible `0` constant.
char *x = 0; // ok in C and C++
char *y = (void*)0; // ok in C, error in C++
char *z = nullptr; // ok in C++
This prevents C++ from defining NULL the way that Posix C does.I don't understand why it's more acceptable to allow magic `0` conversions than magic `(void*)0` conversions, given that the latter is far less likely to happen by accident -- but here we are.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by rightbyte on Monday May 15, 2023 @05:13PM
(#35954564)
> I don't understand why it's more acceptable to allow magic `0` conversions than magic `(void*)0` conversions
In the end you don't have to chose between '0' and 'nullptr' anyways.
char *x = (decltype (nullptr)) 0;
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by kzrdude on Monday May 15, 2023 @03:47PM
(#35953520)
Case in point, integer arithmetic in C. Reasoning about types there is just tiring.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by Dylan16807 on Monday May 15, 2023 @06:06PM
(#35955160)
They didn't say the type system is a problem, they said it caused a problem.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by haileys on Monday May 15, 2023 @07:26PM
(#35955917)
> Nullptr is needed in C++ because `0` is the only definition of `NULL` that works with the type system, due to the lack of implicit `void` conversions.*
> C doesn't have this problem.
Except for conversions between data pointers and function pointers ;)
Initialization seems to be a special case, but with `-pedantic` the following code will show a warning on the initialization of `fp2`:
void* return_null() {
return 0;
}
int main() {
void(*fp1)() = 0;
void(*fp2)() = return_null();
return 0;
}
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by saagarjha on Monday May 15, 2023 @07:41PM
(#35956034)
POSIX requires this to be supported as well.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha...
by whimsicalism on Monday May 15, 2023 @08:53PM
(#35956535)
> due to the lack of implicit `void` conversions. C doesn't have this problem.
implicit void conversions are the problem
Not on a Posix system, where the only valid definition of it is `(void*)0`. C could have adopted this definition.
Nullptr is needed in C++ because `0` is the only definition of `NULL` that works with the type system, due to the lack of implicit `void*` conversions.
C doesn't have this problem.
Adopting the Posix definition of NULL in the standard would have been sufficient -- and unlike `nullptr`, would have solved bugs in existing programs.