News for hackers. Stuff that matters.
Re:How well do you know C++ auto type deduction? by Surac on Monday December 15, 2025 @02:09AM (#46271297)
I always try to avoid auto in c++ or var in C#. On the paper it is a nice was to save you from typing out the type but this is only true if you have tool support and can use your mouse to obtain the type. In printouts or even in a text snippet I’m am lost. I think auto inC++ was the try to shorten some overburden type constructs and make it a little more friendly to use heavy templating. Please forgive my bad English. I’m no native speaker
Re:How well do you know C++ auto type deduction? by majoe on Monday December 15, 2025 @02:30AM (#46271420)
I came to like auto over the years, although I also use it sparingly. Sometimes the concrete types only add visual noise and not much helpful information, e.g. iterators:

auto it = some_container.begin();

Not even once have I wished to know the actual type of the iterator.

Re:How well do you know C++ auto type deduction? by Rucadi on Monday December 15, 2025 @02:49AM (#46271541)
auto has a good perk, it prevents uninitialized values (Which is a source of bugs).

For example:

auto a;

will always fail to compile not matter what flags.

int a;

is valid.

Also it prevents implicit type conversions, what you get as type on auto is the type you put at the right.

That's good.

Re:How well do you know C++ auto type deduction? by feelamee on Monday December 15, 2025 @04:09AM (#46272049)
uninitialized values are not the source of bugs. This is a good way to find logic errors in code (e.g. using sanitizer)
Re:How well do you know C++ auto type deduction? by jb1991 on Monday December 15, 2025 @02:24PM (#46279129)
"bug" can refer to many categories of problems, including logic errors. I've certainly seen uninitialized variables be a source of bugs. Stackoverflow for example is full of discussions about debugging problems caused by uninitialized variables, and the word "bug" is very often used in those contexts.

What do you mean it is not a source of bugs?

Re:How well do you know C++ auto type deduction? by 1718627440 on Monday December 15, 2025 @03:27PM (#46280112)
> What do you mean it is not a source of bugs?

I think what they mean, and what I also think is that the bug does not come from the existence of uninitialized variables. It comes from the USE of uninitialized variables. Making the variables initialized does not make the bug go away, at most it silences it. Making the program invalid instead (which is what UB fundamentally is) is way more helpful for making programs have less bugs. That the compiler still emits a program is a defect, although an unfixable one.

As to my knowledge C (and derivatives like C++) is the only common language where the question "Is this a program?" has false positives. It is certainly an interesting choice.

Re:How well do you know C++ auto type deduction? by feelamee on Monday December 15, 2025 @04:40PM (#46281112)
I mean that bug is *not in* uninitialized variable - bug in program logic. E.g. one of code pathes don't initialize variable.

So, I see uninitialized variables as a good way to find such logic errors. And, therefore, advice to always initialize variable - bad practice.

Of course if you already have a good value to initialize variable - do it. But if you have no - better leave it uninitialized.

Moreover - this will not cause safety issues in production builds because you can use `-ftrivial-auto-var-init` to initialize automatic variables to e.g. zeroes (`-fhardened` will do this too)

Re:How well do you know C++ auto type deduction? by jb1991 on Monday December 15, 2025 @03:02AM (#46271612)
This is indeed exactly correct. Probably on its own this is the most important reason for most people to use it, as I think most of the millions of C++ developers in the world (and yes there are apparently millions) are not messing with compiler flags to get the checks that probably should be there by default anyway. The keyword auto gives you that.
Re:How well do you know C++ auto type deduction? by amelius on Monday December 15, 2025 @03:37AM (#46271835)
Maybe an in-between solution could be a tool that substitutes auto in your code.
Re:How well do you know C++ auto type deduction? by samdoesnothing on Monday December 15, 2025 @02:34AM (#46271441)
Odd, it's not a problem in dynamically typed languages or languages like Kotlin, Swift, etc. I think it's more just what you're used to.
Re:How well do you know C++ auto type deduction? by pjmlp on Monday December 15, 2025 @04:27AM (#46272159)
That is like saying that one rather make fire with sticks and stones than with a lighter, because otherwise one would be lost when going out camping.

IDEs are an invention from the late 1970's, early 1980's.

Re:How well do you know C++ auto type deduction? by gpderetta on Monday December 15, 2025 @04:44AM (#46272291)
Very much true. On the other hand web based review interfaces seem to be stuck in the '60, when they could be so much better if they properly integrated with the compiler.
Re:How well do you know C++ auto type deduction? by pjmlp on Monday December 15, 2025 @05:25AM (#46272619)
The ones on Github with VSCode (Web) integration look quite good already.
Re:How well do you know C++ auto type deduction? by 1718627440 on Monday December 15, 2025 @10:25AM (#46275689)
I think IDEs are a moving target. Do you consider syntax highlighting to make something an IDE? Macro expansion? Autocomplete? intellisense-like based on full text search? based on a parser? based on the compiler? Is Kate an editor or an IDE? It has LSP support.

Having syntax highlighting makes me slightly faster, but I want to still be able to understand things, when looking at a diff or working over SSH and using cat.

Re:How well do you know C++ auto type deduction? by pjmlp on Monday December 15, 2025 @11:39AM (#46276776)
Nope, that is a programmer's editor.
Re:How well do you know C++ auto type deduction? by 1718627440 on Monday December 15, 2025 @02:50PM (#46279511)
I agree, but that means, that even complete integration of the compiler does not make an IDE. So the only distinguishing feature I can think of is startup time /s .
Re:How well do you know C++ auto type deduction? by addaon on Monday December 15, 2025 @03:12PM (#46279880)
An IDE, by name, integrates the development environment. Depending on target, development lifecycle usually includes writing code, generating binaries, pushing those binaries to the target, executing on the target, and debugging on the target; so an IDE includes editing, compiling, linking, device/JTAG drivers, and debugging, in my mind. I suppose for platforms without cross-compilation the device drivers vanish, and for languages without compilers and linkers similar; but at a minimum, development means both authoring and debugging code, and so an IDE must provide an integrated view of the debugging process.
Re:How well do you know C++ auto type deduction? by 1718627440 on Monday December 15, 2025 @03:22PM (#46280026)
Then that distinction doesn't match the original claim, that readability of unlabeled code doesn't matter, since we now have IDEs, as this is an orthogonal matter according to your definition.
Re:How well do you know C++ auto type deduction? by 3836293648 on Monday December 15, 2025 @08:41PM (#46283675)
You can rely on IDE features when GitHub's web view has them
Re:How well do you know C++ auto type deduction? by xnorswap on Monday December 15, 2025 @04:27AM (#46272161)
The guidelines I follow for C# has "use var where it's obvious, but explicitly type when not".

So for example I'd write:

  var x = new List<Foo>();
Because writing:

  List<Foo> x = new List<Foo>();
Feels very redundant

Whereas I'd write:

  List<Foo> x = FooBarService.GetMyThings();
Because it's not obvious what the type is otherwise ( Some IDEs will overlay hint the type there though ).

Although with newer language features you can also write:

  List<Foo> x = new();
Which is even better.
Re:How well do you know C++ auto type deduction? by tcfhgj on Monday December 15, 2025 @05:07AM (#46272485)
I would rename `x` to `foos` and jump to the function/use IDE hints for the exact type when needed.
Re:How well do you know C++ auto type deduction? by Leherenn on Monday December 15, 2025 @07:03AM (#46273394)
Right, although I would argue the most interesting part of the type here is the container, not the containee.

With good naming it should be pretty obvious it's a Foo, and then either you know the type by heart, or will need to look up the definition anyway.

With standard containers, you can have the assumption that everyone knows the type, at least high level. So knowing whether it's a list, a vector, a stack, a map or a multimap, ... is pretty useful and avoid a lookup.

Re:How well do you know C++ auto type deduction? by Aeglaecia on Monday December 15, 2025 @05:27AM (#46272633)
an interesting demarcation of subjective mental encapsulation ... associating the anonymous type of a buffer with the buffer's name ... as opposed to explicitly specifying the type of an anonymously named buffer
Re:How well do you know C++ auto type deduction? by lan321 on Monday December 15, 2025 @10:09AM (#46275467)
I usually prefer

    List<Foo> x = new();
since it gives me better alignment and since it's not confused with dynamic.

Nowadays I only use

    var x = new List<Foo>();
in non-merged code as a ghetto TODO if I'm considering base types/interface.
Re:How well do you know C++ auto type deduction? by binary132 on Monday December 15, 2025 @09:51AM (#46275252)
It’s extremely convenient for certain things. For example, let’s say I’m referring to an enum constant field of a deeply-nested type. This can easily be expressed as “auto k = a.b.c.d.kind” instead of “Alpha::Bet::Charlie::Delta::Kinds k = a.b.c.d.kind”. It should be used sparingly in local contexts where the meaning cannot be confusing.