Live data from Hacker News

Matt Godbolt sold me on Rust by showing me C++

collabora.com

191–200 of 675 posts

Re: Matt Godbolt sold me on Rust by showing me C++

#191
To be fair, this sort of thing doesn't have to be so much worse in C++ (yes, it would have been nice if it had been built into the language itself to begin with). You just need a function to do a back-and-forth conversion which then double-check the results, ie:

  #include   
  #include 

  template 
  void convert_safely_helper_(From const& value, To& result) {
    std::stringstream sst;
    sst > result;
  }

  // Doesn't throw, just fails
  template 
  bool convert_safely(From const& value, To* result) {
    From check;
    convert_safely_helper_(value, *result);
    convert_safely_helper_(*result, check);
    if (check != value) {
      *result = To();
      return false;
    }
    return true;
  }

  // Throws on error
  template 
  To convert_safely(From const& value) {
    To result;
    if (!convert_safely(value, &result))
      throw std::logic_error("invalid conversion");
    return result;
  }

  #include 

  template 
  void sendOrder(const char* symbol, Buy buy, Quantity quantity, Price price) {
    std::cout (buy) (quantity) (price)
            
  void test(Function attempt) {
    try {
      attempt();
    } catch (const std::exception& error) {
      std::cout 
Output:

  sendOrder("GOOG", true, 100, 1000.0): GOOG 1 100 1000
  sendOrder("GOOG", true, 100.0, 1000): GOOG 1 100 1000
  sendOrder("GOOG", true, -100, 1000): GOOG 1 [Error: invalid conversion]
  sendOrder("GOOG", true, 100.5, 1000): GOOG 1 [Error: invalid conversion]
  sendOrder("GOOG", 2, 100, 1000): GOOG [Error: invalid conversion]

Rust of course leaves "less footguns laying around", but I still prefer to use C++ if I have my druthers.

Re: Matt Godbolt sold me on Rust by showing me C++

#192
post #66

I see an article about how strict typing is better, but what would really be nice here is named parameters. I never want to go back to anonymous parameters.

When there are 3-4 parameters it is too much trouble to write the names.

> When there are 3-4 parameters it is too much trouble to write the names.

Sorry, I don't agree.

First, code is read far more often than written. The few seconds it takes to type out the arguments are paid again and again each time you have to read it.

Second, this is one of the few things that autocomplete is really good at.

Third, almost everybody configures their IDE to display the names anyway. So, you might as well put them into the source code so people reading the code without an IDE gain the benefit, too.

Finally, yes, they are redundant. That's the point. If the upstream changes something and renames the argument without changing the type I probably want to review it anyway.

Re: Matt Godbolt sold me on Rust by showing me C++

#194

Yes, Rust is better. Implicit numeric conversion is terrible. However, don't use atoi if you're writing C++ :-). The STL has conversion functions that will throw, so separate problem.

The numeric conversion functions in the STL are terrible. They will happily accept strings with non-numeric characters in them: they will convert "123abc" to 123 without giving an error. The std::sto* functions will also ignore leading whitespace.

Yes, you can ask the std::sto* functions for the position where they stopped because of invalid characters and see if that position is the end of the string, but that is much more complex than should be needed for something like that.

These functions don't convert a string to a number, they try to extract a number from a string. I would argue that most of the time, that's not what you want. Or at least, most of the time it's not what I need.

atoi has the same problem of course, but even worse.

Re: Matt Godbolt sold me on Rust by showing me C++

#195
post #133
post #89

Earlier quoted context omitted.

Google has been doing a very similar, but definitely somewhat uglier, thing with StatusOr and Status (as seen in absl and protobuf) for quite some time. A long time ago, there was talk about a similar concept for C++ based on exception objects in a more "standard" way that could feasibly be added to the standard library, the expected class. And... in C++23, std::expected does exist[1], and you don't need to use excep…

There’s a few backports around, not quite the same as having first class support, though.

I believe the latest versions of GCC, Clang, MSVC and XCode/AppleClang all support std::expected, in C++23 mode.

Re: Matt Godbolt sold me on Rust by showing me C++

#196

The problem I've always had with unit type wrappers is you can't convert between a &[f32] and a &[Amplitude ] like you can convert a single scalar value.

There are libraries that help with these conversions. See e.g.: https://docs.rs/bytemuck/latest/bytemuck/trait.TransparentWr...

Re: Matt Godbolt sold me on Rust by showing me C++

#197

Earlier quoted context omitted.

I apologize for the naive question, but that sounds like a heap?

We have to do arbitrary insertions/deletions from the middle, many of them. I think it is more like BTreeMap, but we need either sorting direction or rev(), and there were some problems with both approaches I tried to solve, but eventually gave up.

I see! The big issue I've run into with BTreeMap is that you can't provide an external comparator. If comparisons only require data that the keys already have, then the Reverse wrapper [1] has worked well for me.

[1] https://doc.rust-lang.org/std/cmp/struct.Reverse.html

Re: Matt Godbolt sold me on Rust by showing me C++

#198
post #30
post #18

Earlier quoted context omitted.

> Implicit numeric conversion is terrible. It's bad if it alters values (e.g. rounding). Promotion from one number representation to another (as long as it preserves values) isn't bad. This is trickier than it might seem, but Virgil has a good take on this ( https://github.com/titzer/virgil/blob/master/doc/tutorial/Nu... ). Essentially, it only implicitly promotes values in ways that don't lose numeric information an…

Aside from the obvious bit size changes (e.g. i8 -> i16 -> i32 -> i64, or f32 -> f64), there is no "hierarchy" of types. Not all ints are representable as floats. u64 can represent up to 2^64 - 1, but f64 can only represent up to 2^53 with integer-level precision. This issue may be subtle, but Rust is all about preventing subtle footguns, so it does not let you automatically "promote" integers to float - you must be…

Yep, Virgil only implicitly promotes integers to float when rounding won't change the value.

     // OK implicit promotions
     def x1: i20;
     def f1: float = x1;
     def x2: i21;
     def f2: float = x2;
     def x3: i22;
     def f3: float = x3;
     def x4: i23;
     def f4: float = x4;

     // compile error!
     def x5: i24;
     def f5: float = x5; // requires rounding

This also applies to casts, which are dynamically checked.

     // runtime error if rounding alters value
     def x5: i24;
     def f5: float = float.!(x5);

Re: Matt Godbolt sold me on Rust by showing me C++

#199
post #79
post #65

Earlier quoted context omitted.

meh, rust is still better cos it’s friendlier

I don’t disagree. Rust learnt a ton from C++. I have my gripes with rust, more it’s ecosystem and community that the core language though. I won’t ever say it’s a worse language than C++.

Could you elaborate on those points, I'm genuinely curious? So far, I have found the Rust community to be immensely helpful, much more so than I experienced the C++ community. Granted, that's quite some time ago and might be at least partially caused by me asking fewer downright idiotic questions. But still, I'm interested in hearing about your experiences.

Re: Matt Godbolt sold me on Rust by showing me C++

#200

What sold me on Rust is that I'm a very bad programmer and I make a lot of mistakes. Given C++, I can't help but hold things wrong and shoot myself in the foot. My media C++ coding session is me writing code, getting a segfault immediately, and then spending time chasing down the reason for that happening, rinse and repeat. My median Rust coding session isn't much different, I also write code that doesn't work, but i…

> Given C++, I can't help but hold things wrong and shoot myself Give an example. I have been programming in C/C++ for close to 30 years and the places where I worked had very strict guidelines on C++ usage. We could count the number of times we shot ourselves due to the language.

Isn't that their point though? They don't have 30 years of C/C++ experience and a workplace with very strict guidelines. They are just trying to write some code, and they run into trouble on C++'s sharper edges.
Post reply on HN