News for hackers. Stuff that matters.
Re:Rust vs. C++ Formatting by tialaramex on Monday January 16, 2023 @03:35PM (#34405164)
It had C's printf, which means it isn't type safe, which is consequently a terrible primitive for this work. Like, it makes sense in C, which thinks boolean is a fancy new concept and thinks 0-terminated strings are a good idea, but it's not actually good.

std::println is more or less what you would obviously build for a modern language and it's notable because C++ could have provided something pretty similar even in C++ 98, and something eerily similar in C++ 11 but it chose not to.

Re:Rust vs. C++ Formatting by hn_go_brrrrr on Monday January 16, 2023 @03:49PM (#34405299)
I think you'll find implementing a type-safe print function in C++11 very challenging. What you could do in constexpr was very limited, type deduction was less powerful, and it didn't have fold expressions. I'd say this really only became feasible since C++17.
Re:Rust vs. C++ Formatting by Kranar on Monday January 16, 2023 @04:01PM (#34405418)
Boost had a type-safe printf function dating back to October 10th, 2002:

https://www.boost.org/doc/libs/1_31_0/libs/format/

Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @04:27PM (#34405707)
And outstreams where not that bad, aswell. Sure, the operator overloading looks a bit rough. But that's IMHO a pragmatic choice if you want to offer customisation points and didn't have variadic functions yet. They where introduced only in c++11.
Re:Rust vs. C++ Formatting by tialaramex on Monday January 16, 2023 @04:34PM (#34405785)
C++ did have variadic functions because it inherited them from C.

What it didn't inherit from C was a way to write variadic functions with variadic types, so that had to be home grown.

Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @04:50PM (#34405941)
Do you mean this feature [0]? I'm not aware of any differences in c and c++ about this. Can you get a type of a argument in C? How? At compile time, or at runtime? Both sound very un-C-like to me. cppreference is usually excellent documentation but it doesn't mention something like this.

I don't considers this to be "proper" variadic arguments, because a functions argument has to have a type. and these, as far as I'm aware of don't have one. This is about a powerfull as passing a void**. This is essentially memcopying multiple differently typed into a char* buffer and then passing that buffer. You can than correctly copies them back you have pretty much the same behaviour. Both methodss obviously lacks important aspects of the language abstraction of a function parameter and i don't what that feature can bring to the table that the previous techniques don't.

[0] https://en.cppreference.com/w/cpp/utility/variadic

Re:Rust vs. C++ Formatting by kevin_thibedeau on Monday January 16, 2023 @05:08PM (#34406098)
> Can you get a type of a argument in C?

You can get it indirectly using _Generic().

Re:Rust vs. C++ Formatting by 1ris on Monday January 16, 2023 @06:06PM (#34406742)
Sorry my bad. Yes, it works for proper function arguments. Does this work for variadic arguments? Parent seems to suggest, but i'm not aware of any mechanism for this.
Re:Rust vs. C++ Formatting by saghm on Monday January 16, 2023 @03:52PM (#34405333)
Maybe the top comment in this thread wasn't so facetious after all then; it did take almost 40 years for C++ to advance enough to have a decent print function
Re:Rust vs. C++ Formatting by tialaramex on Monday January 16, 2023 @04:09PM (#34405504)
Sure, a hypothetical C++ 11 equivalent would e.g. probably not be able to do the trick where we reject bogus formats at compile time, which is something programmers want and which Rust only gets to do by making this a fancy macro whereas C++ 23 is doing it in the actual library code.
Re:Rust vs. C++ Formatting by otherjason on Tuesday January 17, 2023 @07:51AM (#34411829)
fmtlib (https://github.com/fmtlib/fmt), a C++11 formatting library that was the basis for C++20 std::format, is able to do this.
Re:Rust vs. C++ Formatting by tialaramex on Wednesday January 18, 2023 @04:21AM (#34424884)
fmtlib does not, in fact, do this on C++ 11 because as explained it doesn't have enough constant evaluation.

The fmtlib code uses a C-style macro to try to handle format errors at compile time, which detects whether it has enough consteval and no-ops if it does not. On a modern C++ with enough consteval it does exactly what you'd expect it to do, but on older compilers it does nothing.

The result is that fmtlib on an older compiler gets you Exceptions, at runtime, for a format that was invalid at compile time, and if you upgrade the compiler it magically switches to compile time errors.

Edited to add quote from fmtlib docs:

"Compile-time checks are enabled by default on compilers that support C++20 consteval. On older compilers you can use the FMT_STRING macro defined in fmt/format.h instead. It requires C++14 and is a no-op in C++11."

Re:Rust vs. C++ Formatting by bee_rider on Monday January 16, 2023 @07:13PM (#34407392)
Booleans are weird though. A single bit on a system that mostly talks about bytes and bigger? How are you supposed to represent that?
Re:Rust vs. C++ Formatting by jchw on Monday January 16, 2023 @09:40PM (#34408541)
Basically like an enum? It's not that weird. It does not need to use a storage of one bit, just like C chars can technically be larger than eight bits. I suspect a bigger reason that boolean is missing from C is simply because C can't offer much additional value for boolean in its type system.

C++ can obviously do more with boolean types because it's got a more advanced type system, like disallowing values other than true and false to be assigned in code that only uses defined behavior. Boolean itself can take up whatever size the compiler decides it does, same idea as C. But, template types can special case boolean to use bitfields and allow for more space-efficient operations.

Re:Rust vs. C++ Formatting by LegionMammal978 on Monday January 16, 2023 @11:40PM (#34409216)
In the context of Rust, a bool is a 1-byte value with 2 valid representations (0x00 and 0x01) and 254 invalid representations (disregarding uninit bytes). The compiler can make use of these invalid representations for niche optimizations: For instance, an Option<bool> (i.e., std::optional<bool>) takes only 1 byte instead of 2, since the compiler can repurpose the invalid byte 0x03 to represent the None (i.e., std::nullopt) state of the Option. The compiler generally tries to perform these niche optimizations for all enums, although it's only 100% guaranteed to happen in certain cases.
Re:Rust vs. C++ Formatting by gmadsen on Monday January 16, 2023 @08:02PM (#34407833)
i believe this is in cpp23