Back to News
Advertisement
Advertisement

⚡ Community Insights

Discussion Sentiment

60% Positive

Analyzed from 1059 words in the discussion.

Trending Topics

#std#more#auto#vec#language#enumerate#still#python#start#lambda

Discussion (32 Comments)Read Original on HackerNews

galkkabout 1 hour ago
Only people who saw nothing bad in passing pairs .begin(), .end() in tons of places in c++ for like 30 years can say that thing like ‘auto&&’ improves anything.
WalterBrightabout 1 hour ago
C++ should copy D's elegance:

    import std.stdio;

    string[9] vec = [ 
      "the", "quick", "brown", "fox", 
      "jumped", "over", "the", "lazy", "dog" 
    ];

    void main()
    {
      foreach (i, s; vec)
        writeln(i, ": ", s);
    }
WalterBright36 minutes ago
Although D is a strongly typed language, it is very good at type inference. The `s` is inferred as `string`, and `i` as `size_t`.
UncleOxidant38 minutes ago
Reading this, I really can't make much sense of it:

     for (int i=0; auto&& it: vec)
         cout << (++i) << ": " << it << endl;
It's certainly not obvious what's going on there at a glance.

This is at least a bit more pythonic:

     for (auto [i, it] : std::views::enumerate(vec)) {
        std::cout << i << ": " << it << "\n";
     }
grg036 minutes ago
It's probably easier to understand if you read about the "if statement with initializer" linked from the post: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p03...
dataflow25 minutes ago
Not really, I was well aware of if-with-init and I still found this quite confusing. That one actually shows what the variable is initialized to. This one doesn't.
adityamwaghabout 1 hour ago
> It seems to me that the C++ Standards Committee is doing a decent job maintaining the language, and introducing useful features when it makes sense to do so.

This can’t be further from truth. C++ is essentially Frankenstein’s monster.

gregdaniels42143 minutes ago
An articulate creature that hounds its creator for abandoning it?

If anything it is more of a Chimera

saghm40 minutes ago
And the real monster was the one who created it all along!
HarHarVeryFunny3 days ago
C++20 also has an enumerate() generator, so if you like the python syntax you can just do:

for (auto [i,v] : std::views::enumerate(vec)) std::cout << i << ": " << v << std::endl;

FWIW C++23 also has a python-like print and println:

std::println("{}: {}", i, v);

arikrahman38 minutes ago
This is the way I've been doing it and with less hiccups.
BigTTYGothGFabout 1 hour ago
The enumerate is a better solution than the one in the blog post.
Maxatar35 minutes ago
std::views::enumerate is a C++23 feature, not C++20.
WCSTombs3 days ago
The C++20 version is still clearly inferior to the Python and Lua examples because you still have to manually increment the counter in the loop body. IMO the sibling comment by HarHarVeryFunny has a much better C++ equivalent for this idiom, even if it's slightly more verbose.
Gualdrapoabout 2 hours ago
> you still have to manually increment the counter in the loop body

It doesn't look like that to me, the ++i thing seems to be just to start printing the array from 1 (I don't know how things are in Python nowadays but I know in Lua arrays start at 1, so there's no need for something like this in there), the value of i is still increasing without telling it explicitly to do so

sheeptabout 2 hours ago
Python is 0-indexed. In OP's example, the start parameter makes i start at 1.

Their C++17 example prints starting from 0. Probably a mistake.

If you look at the linked page for C++20[0], other types can be put in the initializer statement, so it's unlikely the loop auto-increments.

[0]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p06...

kbensonabout 1 hour ago
So, it's just a while loop in a for loop's clothing? That's not going to be confusing for people at all...
moron4hireabout 2 hours ago
No, the increment is necessary to keep the counter going. The choice of pre-increment versus post-increment handles the "start at 1" issue you mentioned, but it isn't auto incrementing.
jandrewrogers24 minutes ago
As someone who likes and uses modern C++ this seems like a very marginal improvement. I see the use case but it seems rather unimportant.
kazinator36 minutes ago

  1> (mapdo (op put-line `@1: @2`) '#"how now brown cow" 0)
  how: 0
  now: 1
  brown: 2
  cow: 3
  nil
mapdo: a mapping function for side effects of calling the function, not calculating a result, like map does.

op: produce a lambda expression out of an expression in which @1, @2, ... explicitly indicate the insertion of positional arguments, which are implicitly collected and become the parameter list of the lambda.

`...`: quasistring syntax: supports @ notations for interpolating. The @1, @2 elements of op do not require a double @@ inside a quasistring.

put-line: ordinary function to put a string to a stream (standard output by default) followed by newline. The lambda generated by op contains a (put-line ...) expression as its body, with @1 and @2 transformed into references to to generated, unique parameter names.

#"...": string list literal: contents are broken on whitespace and denote a list of strings #"foo bar" -> ("foo" "bar"). Requires ' quote in front to be quoted literally, and not evaluated as a compound expression applying the argument "bar" to the operator "foo". Yes, there is a #`...` quasi string list for templating over this.

nil: the value returned by mapdo after the side effects, printed by the REPL, not part of the output.

0: ordinary integer zero. But endowed with the power of being iterable. Where an iterable thing is required, 0 denotes the whole numbers 0, 1, 2, ... Similarly, 42 denotes 42, 43, ...

These are some of the ingredients produced by my one-member research programme into nicer Lisp coding.

  2> (map (ret `@1: @2`) '#"how now brown cow" 0)
  ("how: 0" "now: 1" "brown: 2" "cow: 3")
map: take tuples by iterating over argument iterables in parallel, pass them to a function to project each tuple to a value, then return a list of values.

ret: cousin of op built on the same framework as op. Used for turning an expression into a lambda, when the expression isn't a compound form with an obvious operator. To turn (foo bar) into a lamdbda with op we use (op foo bar). But what if we have a simple variable x and want (lambda () x)? (op x) is not right, it means (lambda () (x)). (ret x) provides the sugar. Here, it lets us spin up a two-argument function that evaluates a quasistring.

HeavyStormabout 1 hour ago
Given both Lua and python use a enumerator, the C++ example isn't really the correct equivalent.

  for (auto [i, v] : std::views::enumerate(vec)) {
      std::cout << i << ": " << v << '\n';
  }
This is how you'd do it.
drnick134 minutes ago
Give me the explicit C syntax any day over this monstrosity. I refuse to write anything other than C++98, the last version of C++ that built on C without trying to turn it into a completely different language.
Gualdrapoabout 2 hours ago
The way this website shows the programming languages is odd. They're blue and slanted and if you hover your mouse cursor over them they have a color transition, so you'd think they are links - and yet you click on them and nothing happens
Advertisement
hackthemack29 minutes ago
I guess I am officially old and no longer know what the cool "it" is any more. The C++17 example seems much more clear to me. It is more typing, but so what? It is not that much more typing, and it looks like code that people have written for 40 years.
dvtabout 1 hour ago
I am so happy I haven't written a line of C++ in like 15 years. Absolutely disgusting language. Every time I look up one of their new standards, I'm like how does anyone keep all of this in their head (usually on top of stuff like boost, etc.)? No wonder LLMs are a thing.
lioeters36 minutes ago
C++ is a decades-long mistake that spawned other misguided directions in the entire software industry. We're still trying to undo many of the bad ideas, but I'm afraid even Rust has too much of C++ in it, not only in terms of syntax but mentality and culture. LLMs are making things worse by automatically generating code in such bloated languages, where people have less need to even look at the horrific spaghetti inside. The whole thing needs to be reconsidered from the ground up, from first principles, by actual creative thinking human beings with lessons of hindsight.

> Gall's Law: A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over with a working simple system.

fourseventyabout 2 hours ago
Just what C++ needs, more bloat lol.
petilonabout 2 hours ago
C++ is probably the only language that needs a strong style guide, to define a subset you are allowed to use in the project.

Linus Torwalds famously said that subset is zero. You're not allowed to use C++ in Linux, a wise move.

Here's Google's: https://google.github.io/styleguide/cppguide.html Search for "do not use" and you'll find plenty of hits.

bigcityslider24 minutes ago
And it's not because he's cranky or something, he was holding out for something worthy to possibly use instead of C. Turned out to be Rust.
tcfhgjabout 1 hour ago
> C++ is probably the only language that needs a strong style guide,

here you go: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines

zeroq40 minutes ago
Good Lord.

Reminds me of Good Parts, Bad Parts meme of JS.