Earlier quoted context omitted.
Lack of ergonomics around static_cast is language problem. The "most safe" of the casts should not be this verbose.
I see your point. It would be nice if a C-style cast behaved like a static_cast in C++, but that's not possible because of backwards compatibility. At least with GCC/Clang you can disable C-style casts altogether with '-Werror=old-style-cast'. (Maybe there is a similar option for MSVC.)
C constructs that still don't work in C++
161–170 of 182 posts
Re: C constructs that still don't work in C++
#162Earlier quoted context omitted.
That's what I addressed in the first paragraph, a warning in C is a serious thing, not just some random nuisance.
Ok, but then compilers should really throw errors for these diagnostics by default. Why lump serious issues like incompatible pointer casts together with benign things like uninitialized variables? (Another pet peeve of mine is that missing returns in functions is not considered a hard error. GCC 16 does not even issue a warning by default when compiling C code, which is just crazy. "-Werror=return-type" is the first…
That's because the default mode for compilers is to just shut up and compile this throw away code. Programmers know that you are expected to turn the warnings on and also nobody is manually invoking compilers directly. The default for build systems is often "-Wall -O2" anyway. Personally I prefer the status quo, I hate tools, that I just invoke and they start spamming my terminal. "Like, I know, that there will be issues, that's why I'm using you, but you don't need to tell me until I told you what exactly you are supposed to report me. Don't spam me until I ask you."
> Why lump serious issues like incompatible pointer casts together with benign things like uninitialized variables?
?? Uninitialized variables are also serious issues and also undefined behaviour. If anything they're more serious, because for pointer casts, it is likely that the types are compatible, but the compiler just doesn't know it, while truly uninitialized variables are always a real error. For the latter, the issue is rather, that the compiler often can't proof whether the variables are actually uninitialized. That's why it is more likely to meet a "maybe-uninitialized", a true "uninitialized" is rare in the wild.
Re: C constructs that still don't work in C++
#163Earlier quoted context omitted.
Not necessarily. In pure theory, yes. But in pure theory, monomorphized code can be deduplicated too. Monomorphization often forces types information to flow, whereas type-erasure considers that a bonus. As such, a compiler may accidentally introduce opaque boundaries that hide type information. Even in cases where this is the programmer's fault and the type info is truly lost, in monomorphisation, that becomes an ex…
The point is much easier to propagate information (and not accidentally lose it) than to deduplicate after specialiation, so I do not agree that this is only of theoretical relevance. I agree that monomorphization has explicit rules that force specialization, I do not think this is an advantage at all because it is too rigid and one can introduce explicitness at the language level also for devirtualization where this…
Monomorphization isn't premature optimizations either. It and type-erasure are semantically different. For instance, only monorphization can enable inlining of data structures (as devirtualizating an array of fat pointers involves dealing with ownership concerns, among other things). You either have to type-erase the array itself or box all the items, none of which are very ergonomic for a system-level programming. Also, in terms of performance, monomorphization generally makes the compiler's job easier overall in comparison to type-erasure (specially in Rust where types also encode lifetime), so I don't see the damage unless you are dealing with an extremely size-constrained environment or dealing with instruction cache misses (which is an indication of poor abstractions).
Finally, as stated elsewhere, it's trivial to implement any type-erasure polymorphism in a monomorphic system (save for very specific exceptions, related to hardware), but the reverse is not true. To do so reliably will require assistance of an external codegen tool, like the C macros, generally recreating monomorphism, but worse, as the compiler doesn't actually know the polymorphic code is related, making deduplication even harder.
Re: C constructs that still don't work in C++
#164Earlier quoted context omitted.
The point is much easier to propagate information (and not accidentally lose it) than to deduplicate after specialiation, so I do not agree that this is only of theoretical relevance. I agree that monomorphization has explicit rules that force specialization, I do not think this is an advantage at all because it is too rigid and one can introduce explicitness at the language level also for devirtualization where this…
You seem to be underestimating how easy it is to lose type information. Creating a array of type-erased is already enough to cause confusion, even if the surrounding code is enough to prove the array only has one type. If we are to assume perfect deductive ability for type-erasure, why not for monomorphism as well? Monomorphization isn't premature optimizations either. It and type-erasure are semantically different.…
Re: C constructs that still don't work in C++
#165Earlier quoted context omitted.
You seem to be underestimating how easy it is to lose type information. Creating a array of type-erased is already enough to cause confusion, even if the surrounding code is enough to prove the array only has one type. If we are to assume perfect deductive ability for type-erasure, why not for monomorphism as well? Monomorphization isn't premature optimizations either. It and type-erasure are semantically different.…
I don't see why it would easy to lose type information in circumstances where you can do monomorphization. Note that I do not assume "perfect deductive ability". The practical observation is simply that it is far more effort to deduplicate than to clone.
From the code, we can already tell all elements have the same type, but the compiler is already adding checks. One of the options would be to convert the array of fat pointers into a fat slice. But now you have to enlarge the slice's vtable with all possible item operations, even if we don't use them often. Alternatively, use monomorphism. Or use both.
Re: C constructs that still don't work in C++
#166Earlier quoted context omitted.
Ok, but then compilers should really throw errors for these diagnostics by default. Why lump serious issues like incompatible pointer casts together with benign things like uninitialized variables? (Another pet peeve of mine is that missing returns in functions is not considered a hard error. GCC 16 does not even issue a warning by default when compiling C code, which is just crazy. "-Werror=return-type" is the first…
> Ok, but then compilers should really throw errors for these diagnostics by default. That's because the default mode for compilers is to just shut up and compile this throw away code. Programmers know that you are expected to turn the warnings on and also nobody is manually invoking compilers directly. The default for build systems is often "-Wall -O2" anyway. Personally I prefer the status quo, I hate tools, that I…
That's just not true. Compilers do issue certain warnings by default and they even treat some warnings as errors. See https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
> ?? Uninitialized variables are also serious issues and also undefined behaviour.
Oh my, I meant to write "unused variables" facepalm. Uninitialized variables are of course a serious issue.
So back to my original point: you said that warnings in C are always a serious thing, but that's not really true. Warnings can range from more or less harmless to very serious. In fact, compilers recognize this issue by upgrading certain warnings to errors by default. See again my link to the GCC 14 post: https://gcc.gnu.org/gcc-14/porting_to.html.
Re: C constructs that still don't work in C++
#167Earlier quoted context omitted.
It didn't stand out to me on first read but pangram gives it a high confidence rating as being written by AI.
It's much more obvious when you look at the user's comment history. Curiously my comment above was on +3 karma last time I looked, but now it's on -2. It seems like the median HN user is getting worse at slop detection (or is otherwise ambivalent towards slop comments).
Re: C constructs that still don't work in C++
#168Earlier quoted context omitted.
> Ok, but then compilers should really throw errors for these diagnostics by default. That's because the default mode for compilers is to just shut up and compile this throw away code. Programmers know that you are expected to turn the warnings on and also nobody is manually invoking compilers directly. The default for build systems is often "-Wall -O2" anyway. Personally I prefer the status quo, I hate tools, that I…
> That's because the default mode for compilers is to just shut up and compile this throw away code. That's just not true. Compilers do issue certain warnings by default and they even treat some warnings as errors. See https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html > ?? Uninitialized variables are also serious issues and also undefined behaviour. Oh my, I meant to write "unused variables" facepalm . Uninitia…
Because some warnings are just believed to be more serious, than others.
> In fact, compilers recognize this issue by upgrading certain warnings to errors by default.
Which I interpret as a culture shift, due to people complaining about it and also because the contemporary meaning and expectation of warnings for programming languages has shifted.
Re: C constructs that still don't work in C++
#169Earlier quoted context omitted.
> That's because the default mode for compilers is to just shut up and compile this throw away code. That's just not true. Compilers do issue certain warnings by default and they even treat some warnings as errors. See https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html > ?? Uninitialized variables are also serious issues and also undefined behaviour. Oh my, I meant to write "unused variables" facepalm . Uninitia…
> That's just not true. Compilers do issue certain warnings by default and they even treat some warnings as errors. Because some warnings are just believed to be more serious, than others. > In fact, compilers recognize this issue by upgrading certain warnings to errors by default. Which I interpret as a culture shift, due to people complaining about it and also because the contemporary meaning and expectation of war…
Yes, that is my entire point. For some reason you were arguing against it.
Re: C constructs that still don't work in C++
#170Earlier quoted context omitted.
> That's just not true. Compilers do issue certain warnings by default and they even treat some warnings as errors. Because some warnings are just believed to be more serious, than others. > In fact, compilers recognize this issue by upgrading certain warnings to errors by default. Which I interpret as a culture shift, due to people complaining about it and also because the contemporary meaning and expectation of war…
> Because some warnings are just believed to be more serious, than others. Yes, that is my entire point. For some reason you were arguing against it.
> Also, let's not forget that implicit casts between unrelated pointer types is only a warning in C.
I intended to argue against the "only a warning", because the meaning of a warning in C is something very serious. That is different from more modern languages, who may use this to inform you that you are no longer writing idiomatic code. Of course there are also exceptions, that you often need to enable explicitly. But the fact that something is a warning in C, doesn't mean it's nothing serious, quite the opposite. That's my point.