Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
You mean like a function that takes a length and int, like say something like this

code:
char binvar[5];
binvar = decimal2bin(5, 12);
Get back 1,1,0,0,'\0' ?

Adbot
ADBOT LOVES YOU

Qwertycoatl
Dec 31, 2008

His Divine Shadow posted:

You mean like a function that takes a length and int, like say something like this

code:
char binvar[5];
binvar = decimal2bin(5, 12);
Get back 1,1,0,0,'\0' ?

It'd have to take the array, something like:
code:
char binvar[5];
decimal2bin(binvar, 5, 12);

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
Oh yeah, gotcha.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

A C++ behaviour that has stumped someone who wrote a time-travelling debugger:

https://twitter.com/rocallahan/status/1643340192034742272

I can’t figure out how to copy from godbolt on my phone, so here’s a screenshot:

Presto
Nov 22, 2002

Keep calm and Harry on.
I think any compiler should refuse to compile that, out of principle.

Absurd Alhazred
Mar 27, 2010

by Athanatos

Subjunctive posted:

A C++ behaviour that has stumped someone who wrote a time-travelling debugger:

https://twitter.com/rocallahan/status/1643340192034742272

I can’t figure out how to copy from godbolt on my phone, so here’s a screenshot:



This is an affront to God, is what it is.

OddObserver
Apr 3, 2009

Presto posted:

I think any compiler should refuse to compile that, out of principle.

I am sure there is some very well-thought out reason that this doesn't throw an error at the X enumerator, but sometimes people who get too deep into details end up missing some perspective....

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
I can't even tell if the question is "is it a compiler bug to compile this" or "is it a compiler bug to fail to compile this".

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Presto posted:

I think any compiler should refuse to compile that, out of principle.

Absurd Alhazred posted:

This is an affront to God, is what it is.

OK sure but I think we’ve established that C++ is outside the bounds of conventional spirituality and morality, so I’m looking for a more secular answer

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

roomforthetuna posted:

I can't even tell if the question is "is it a compiler bug to compile this" or "is it a compiler bug to fail to compile this".

I’ll take either! I’m not picky.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Subjunctive posted:

I’ll take either! I’m not picky.
Then yes!

Absurd Alhazred
Mar 27, 2010

by Athanatos

Subjunctive posted:

OK sure but I think we’ve established that C++ is outside the bounds of conventional spirituality and morality, so I’m looking for a more secular answer

C++ is proof that I, personally, am in hell.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

rjmccall is going to have a couple of drams and pop in here and drop chapter-and-verse on us and this place is the best :10bux: I have ever spent

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Ordinary names and tag names can coexist in the same scope. In C, they are completely independent namespaces, and you have to write a tag specifier to resolve a tag name, e.g. struct foo. C++ wanted to let you write class names without the extra keyword, so ordinary lookup can find tag names, but in an effort to preserve C compatibility, tag names are hidden by ordinary names in the same scope unless you’re doing tag lookup specifically. Naturally this rule in C++ also applies in a million situations that are impossible to write in C, like qualified lookup.

Anyway, GCC is loving up the lookup performed for an elaborated type specifier, which needs to be a tag lookup and so ignore non-tag names. Probably they’re failing to preserve the fact that this is an elaborated type specifier when they record the dependent name within a template.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Subjunctive posted:

rjmccall is going to have a couple of drams and pop in here and drop chapter-and-verse on us and this place is the best :10bux: I have ever spent

Goddamnit

…I’m not looking up the citations

Presto
Nov 22, 2002

Keep calm and Harry on.

I've been programming C++ for over 20 years now, and I have no idea what you just said.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Does that mean struct Foo::X should be resolving to the enum that has shadowed the struct? In this context is it even possible to specify struct Foo::struct X?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

roomforthetuna posted:

Does that mean struct Foo::X should be resolving to the enum that has shadowed the struct? In this context is it even possible to specify struct Foo::struct X?

No, AIUI `struct Foo:X` is an explicit tag name lookup which should ignore the enum.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Is there a legit reason to do that, whatever it is?

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Subjunctive posted:

No, AIUI `struct Foo:X` is an explicit tag name lookup which should ignore the enum.
Not sure I get it. Foo::X would refer to the enum then, because it's not explicitly a tag, but then if you had something shadowing Foo, so you have to specify struct Foo, then it would become impossible to reference the enum?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

roomforthetuna posted:

Not sure I get it. Foo::X would refer to the enum then, because it's not explicitly a tag, but then if you had something shadowing Foo, so you have to specify struct Foo, then it would become impossible to reference the enum?

Parentheses I imagine? (struct Foo)::X vs default of struct (Foo::X)? I dunno, I’m just trying to be so egregiously wrong that rjmccall steps in again.

OddObserver
Apr 3, 2009

Presto posted:

I've been programming C++ for over 20 years now, and I have no idea what you just said.

Have you ever seen old C code that does something like: "typedef struct X {} X;"?

Well, that's because in C if you just do "struct X {};" you can't do "X var;" it has to be "struct X var;" which is kinda verbose.... And C++ did some weird thing where you can write "X var;" but it still cares about the distinction sometimes.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

roomforthetuna posted:

Does that mean struct Foo::X should be resolving to the enum that has shadowed the struct? In this context is it even possible to specify struct Foo::struct X?

struct Foo::X is an elaborated type specifier, in this case using this production:

code:
elaborated-type-specifier ::= class-key attribute-specifier-seq? nested-name-specifier? identifier
class-key is just one of the three tag keywords: struct, class, and union. (enum elaborated type specifiers use a different but almost identical production for no particularly good reason.) There are no attributes in this specifier. nested-name-specifier is a qualifier which always ends in ::. Finally, the identifier is X. So you need to parse this as:

code:
struct     Foo::     X
The special lookup rule for elaborated type specifiers only applies to the lookup of the final identifier, in this case X. Non-template names in nested name specifiers (i.e. identifiers preceding a ::) use their own special lookup rule which ignores everything except types and namespaces. So to process struct Foo::X, the compiler immediately does an unqualified nested-name-specifier lookup for Foo and finds the struct declaration. It then immediately tries to do a qualified tag lookup into that scope for X. In that scope, it finds the enumerator, but individual enumerators are not tags and it is performing tag lookup, so it ignores that; it also finds the struct, and structs are tags, so it resolves to that. It then checks that the class-key in the elaborated type specifier (struct) matches the tag declaration (also struct), and it does, so the lookup is okay and this elaborated type specifier has been resolved.

A microsecond earlier, the compiler was processing the struct Base::X in the template definition. It immediately did an unqualified nested-name-specifier lookup for Base and found the template type parameter. It then immediately tried to do a qualified tag lookup into that scope for X, but it could not because the scope specifier was dependent. So instead it wrote down that it had a dependent type name, including all the relevant details of the lookup like the identifier X and the dependent scope Base::.

When the compiler instantiates the template definition for Base=Foo, it sees the record of the dependent type name and tries to instantiate it. First, it must try to resolve the dependent scope Base:: to something concrete, which it does by resolving the template parameter Base to the corresponding template argument Foo. It can now do a qualified lookup into that scope. It is supposed to do a qualified tag lookup, which should resolve exactly like it did in the non-dependent case. However, I'm assuming they forgot to write down that the elaborated type specifier was written with struct, so instead the compiler is resolving the name as if it were written with typename, which does not do a tag lookup and so resolves to the enumerator and dies because that's not a type.

rjmccall fucked around with this message at 03:56 on Apr 5, 2023

Xarn
Jun 26, 2015
I wish to subscribe to the "rjmccall explains C++" service.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
C++ code looks so loving incomprehensible to me compared to regular C.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Xarn posted:

I wish to subscribe to the "rjmccall explains C++" service.
Yeah, that was a really great explanation that semi-ordinary humans can understand. Thanks rjmccall!

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

His Divine Shadow posted:

C++ code looks so loving incomprehensible to me compared to regular C.

Depends how up their own rear end the author is about template metaprogramming. But yeah, C is a great language.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Xarn posted:

I wish to subscribe to the "rjmccall explains C++" service.

roomforthetuna posted:

Yeah, that was a really great explanation that semi-ordinary humans can understand. Thanks rjmccall!

:tipshat:

Presto
Nov 22, 2002

Keep calm and Harry on.

His Divine Shadow posted:

C++ code looks so loving incomprehensible to me compared to regular C.

Tbf that example is a pathological case and if you submitted a PR with that code in my workplace people would be asking what kind of drugs you were on.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

ultrafilter posted:

Is there a legit reason to do that, whatever it is?

Having a nested type and an enum case with the same name isn't a good idea, but since it works it seems like an unsurprising thing to have just incidentally happen given enough code written with a naming convention that makes it possible.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
The most annoying naming conflicts I generally run into are all in union-like situations where, yeah, it’s not uncommon to want to name a nested type, a union field, and an enumerator all the same thing: a nested type to abstractly represent what I want to store in some case, a union field to actually add the storage for that case, and an enumerator in an enum that I store separately to say which case I’m in. I have the misfortune of working in codebases with a legacy of capitalizing local variable and field names, though.

Computer viking
May 30, 2011
Now with less breakage.

His Divine Shadow posted:

C++ code looks so loving incomprehensible to me compared to regular C.

C++ is a nifty expansion of C if you just limit yourself to the right parts. It can also be a fairly clean modern language if you just stick to those parts. The problem is that nobody agrees on which parts are the good ones, and you can't entirely avoid the rest.

I rarely reach for C++; if I want a low level language I'll stick to C, and if I want a modern language there are multiple alternatives that don't have more corner cases than a hedge maze. It's the best way to write Qt and fast R libraries, I guess?

(Why am I in this thread if I'm so ambivalent to C++? Idk, I like C and hope to absorb some C++ by immersion?)

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


No, no, ambivalence is about right.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Computer viking posted:

C++ is a nifty expansion of C if you just limit yourself to the right parts. It can also be a fairly clean modern language if you just stick to those parts. The problem is that nobody agrees on which parts are the good ones, and you can't entirely avoid the rest.

I rarely reach for C++; if I want a low level language I'll stick to C, and if I want a modern language there are multiple alternatives that don't have more corner cases than a hedge maze. It's the best way to write Qt and fast R libraries, I guess?

(Why am I in this thread if I'm so ambivalent to C++? Idk, I like C and hope to absorb some C++ by immersion?)

Turn it into the C fanclub thread with me.

pseudorandom name
May 6, 2007

C++ turned away from the face of god when it stepped beyond C With Classes.

You could probably make a pretty decent language of it if you copied Swift's dynamic object layout thing and also made it more COM-like.

Foxfire_
Nov 8, 2010

ultrafilter posted:

Is there a legit reason to do that, whatever it is?
No right thinking people would do it on purpose, but the rules (created for the sake of other behavior) will still cover what is required in any situation and here happen to specify a particular thing instead of "compiler must error", "you may not do this, compiler is not required to warn you" (undefined behavior), or "compiler's choice, consult their documentation" (implementation defined).

Adding a new rule just for the sake of making this be an error would be more complexity (+ technically break old code that did it, probably accidentally)

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


C/C++ Thread: No right thinking people would do it on purpose

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Computer viking posted:

C++ is a nifty expansion of C if you just limit yourself to the right parts. It can also be a fairly clean modern language if you just stick to those parts. The problem is that nobody agrees on which parts are the good ones, and you can't entirely avoid the rest.

my problem with c++ is that I don't like c++ templates. i like generics the way they were done in later languages, but C++ templates are loving awful - they make compile times slow, they're slow to parse, and template metaprogramming is the most incomprehensible bs you'll ever see. unfortunately, you may have noticed that the stl is the "standard template library"... it's very difficult to enjoy programming in a language when you disagree with its foundation.

Xarn
Jun 26, 2015
Templates are awful, and I don't know anyone who really likes them. But then you use the modern replacement like Rust's traits and suddenly realize that you flat out cannot do things that would be simple in C++ (see e.g. variadic template packs).

Adbot
ADBOT LOVES YOU

go play outside Skyler
Nov 7, 2005


Computer viking posted:

C++ is a nifty expansion of C if you just limit yourself to the right parts. It can also be a fairly clean modern language if you just stick to those parts. The problem is that nobody agrees on which parts are the good ones, and you can't entirely avoid the rest.

I rarely reach for C++; if I want a low level language I'll stick to C, and if I want a modern language there are multiple alternatives that don't have more corner cases than a hedge maze. It's the best way to write Qt and fast R libraries, I guess?

(Why am I in this thread if I'm so ambivalent to C++? Idk, I like C and hope to absorb some C++ by immersion?)

I use C++ a lot because you can mix and match it with C, and C has interoperability with tons of languages like Swift, Objective C, Kotlin, Golang, etc. This makes it still a top choice for anything that needs to be cross-platform on the long term.

As you said though, you need to stick to the good parts. And you have to know all of them to make the right choices. I learned about std::execution::par just a few months ago for example. It's got a very rich standard library.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply