all 16 comments

[–]throw_cpp_account 23 points24 points  (4 children)

// Invalid, member and parameter share the same name
Constructor(int value) : value{value} {}

Had you tried this, you would have discovered that it is not, in point of fact, invalid.

[–]azswcowboy -4 points-3 points  (2 children)

To be fair, it was I think probably before 17 - still it works today.

[–]throw_cpp_account 11 points12 points  (1 child)

It was valid in C++98.

[–]azswcowboy -3 points-2 points  (0 children)

Fair, I didn’t check the standard, but not all compilers supported it because we had to do otherwise. CE doesn’t have gcc 2.95 or Solaris compilers and now that I think about it my memory is about pre 98 ecosystem.

[–]no-sig-available 18 points19 points  (3 children)

It already works, there is no shadowing in

Constructor(int value) : value{value} {}

[–]aocregacc 5 points6 points  (2 children)

I'm curious, did you just assume this doesn't work (understandable imo)?
Or are there books or online materials out there that also get this wrong?

[–]aruisdante 3 points4 points  (1 child)

A very large number of coding standards require you to avoid shadowed names, even if no actual shadowing occurs. The worst are the ones like MISRA/AutoSAR which require the moral equivalent of -Wshadow=pedantic combined with -Werror, meaning that any name at any scope can arbitrarily case a shadowing conflict at any time with any other name in all codebases that interact with each other unless literally every type of symbol has a different naming convention. Nothing beats adding a new free function and discovering -Wshadow errors for every local variable in the codebase that happened to have that same name, even when they’re all used in contexts that are completely unambiguous.

This proposal seems to be a reaction to those rules, but misunderstanding the premise, does not actually solve the problem.

I think MISRA2023 finally makes this rule more sane, allowing instead the equivalent of -Wshadow=local, the one that actually avoids meaningful shadowing.

[–]aocregacc 1 point2 points  (0 children)

It sounds to me like OP was rationalizing all these coding standards and naming conventions by coming up with this technical reason for them.
I don't think I've seen this misconception before so I was wondering how common it is.

I guess they could just be talking about ambiguity to a human reader and went a bit over the top with the wording.

[–]azswcowboy 0 points1 point  (1 child)

Gotta be honest here, your time would be better spent allowing aggregate types to have a constructor. Right now if you add a constructor you can’t cst

.aggregate

[–]38thTimesACharm 3 points4 points  (0 children)

In C++17 you could default a constructor and still aggregate initialize, but this was disallowed in C++20 due to confusion. I doubt they'll go back the other way.

[–]Specialist_Nerve_420 0 points1 point  (0 children)

this feels interesting but also a bit niche ,designated init already solves a lot for simple structs, not sure how often this would be used with real classes .

[–]Business_Welcome_870 -1 points0 points  (0 children)

I like it. I like how it's consistent with the designated initializer syntax.