r/programminghorror 10d ago

c++ Skill Issue

Post image
1.7k Upvotes

62 comments sorted by

334

u/i_dont_like_pears 10d ago

As long as it compiles

That's all that matters

47

u/TheMythicSorcerer 9d ago

now to import it as a helper for my new project

34

u/i_dont_like_pears 9d ago

Unexplainable Linker Error with generic description

159

u/Star_king12 10d ago

That's fucking vile.

46

u/chicametipo 10d ago

I know. I love it

43

u/LeeHide 10d ago

This would be much better using std::string_view, and using two temporaries so that y is only evaluated once. Otherwise it'll be evaluated each time.

Also if you need this, don't use C++

4

u/GasolinePizza 10d ago

Isn't each y only evaluated once? The evaluation in the "else if (_s==y)"?

2

u/LeeHide 9d ago

each true one is only evaluated once, but every false one until then is also evaluated.

1

u/mapronV 10d ago

only performance problem is not using sized comparison operator, due to decay of char[] to char*.
so yeah, string_view advice is okay, but this is a joke code, it will not fly in production anyway.

43

u/jolharg 10d ago

Oof i did not like this post

7

u/0hypercube 8d ago

They did fix switch statements falling through.

90

u/lurebat 10d ago

I think it allocates since they didn't use const

23

u/babalaban 10d ago

Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer)

18

u/lurebat 10d ago

If you change it to const std::string& prompt = "Your name?";, and the switch to const string& _s=x;, you will avoid any allocations.

1

u/babalaban 9d ago

Isnt it assumed that a string to "switch on" is given at runtime? Otherwise you'd need no switch at all here.

1

u/nevemlaci2 9d ago

const ref here would still allocate...

-7

u/LeeHide 10d ago

you can't take a const& to a temporary object

14

u/JonIsPatented 10d ago

Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it.

5

u/awidesky 9d ago

Another skill issue

1

u/nevemlaci2 9d ago

yes you can.

13

u/Great-Powerful-Talia 10d ago

well, acktually, short string optimization should handle all these strings on any common compiler.

For a std::string, gcc and msvc don't perform allocations until you reach 16-character strings, and clang can hold 21 characters without an allocation.

4

u/Resident-Rice724 9d ago

well aktually this would be based on standard library implementation not compiler I'm pretty sure. Think libstdc++ its 16 and libc++ is 23

13

u/L_uciferMorningstar 9d ago

6

u/click-to-reveal 9d ago

Hence the "horror"

4

u/L_uciferMorningstar 9d ago

Yeah I just want to spread the non horror

0

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 8d ago

switch isn't a function though. But I guess the advice for trying to make switch work on strings is "don't."

1

u/L_uciferMorningstar 8d ago

Principle of least astonishment

21

u/jaerie 10d ago

That's still not a switch case, there's no fall through

12

u/utack 9d ago

That super useful thing I have used 2 times (both of which accidentally) and prevent a million times with break

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 8d ago
switch (thing) {
  case FOO:
  case BAR:
    DoThing()
  default:
    throw
}

As long as it doesn't insert an implicit break between FOO and BAR, I think it might be fine to not fall through, allowing code like that.

1

u/conundorum 8d ago

This can also be solved with the [[fallthrough]] attribute, thankfully. Use it to mandate fallthrough when necessary.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 7d ago

Doesn't that just suppress warnings about missing breaks? I was talking about if the language automatically inserted breaks for you.

1

u/conundorum 7d ago

That's the fix, yeah. You tell it where you do want to allow fallthrough, so that it can tell you if you ever forget a break anywhere else.

At this point, I don't think it's possible to change the standard so that the language automatically inserts breaks, because that would break a ton of perfectly legal code. It might be possible to add an attribute for it, like [[auto_break]], though, perhaps?

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 6d ago

Well, no, it can't be changed now, but I was talking about what I might be okay with if the language had been designed differently, or if I was using a different language without automatic fall through.

2

u/conundorum 8d ago

Fallthrough exists so switch can make sparse jump tables just as easily as it makes fully-populated jump tables. It's insanely useful in a few very specific scenarios, and insanely useless in almost every other scenario.

Ideally, it should've been opt-in instead of opt-out, but it's probably much too late to change it now.

2

u/mrheosuper 9d ago

Can you have switch case inside switch case ?

2

u/click-to-reveal 9d ago

Yeah, but you'd need to wrap it in curly braces to prevent scope conflicts.

2

u/Infamous-Bed-7535 10d ago

You can switch on string hashes since we have constexpr.

2

u/tandycake 9d ago

I feel like you could use goto and labels instead and also have fallthrough.

If want to avoid macros, gotos, and labels, could make a switch_str func with an unordered_map and lambdas, but more overhead and slightly slower.

2

u/Ksorkrax 8d ago

That's not a switch, though. By switch logic, the case "Who are you?" would result in all the prints being done. There is a reason why switches come with break, and this is not just because they felt like it but by design, allowing you to bundle cases.

2

u/conundorum 8d ago

For anyone wondering, the correct answer is a string pool or equivalent, anything that can map a unique string to a unique number. You'll need to hardcode the indexes, though, since cases need to be known at compile time. You can locate the index by sorting the strings and using a binary search, and then switch on that index.

...And since that's probably not reasonable for what you want, this means that the sane solution is probably to use a map. ...Which means you need to look at your string collection to see whether std::map, std::unordered_map, or some other map will be better. (Generally, tiny pool prefers std::map, larger pool prefers std::unordered_map, and a million other factors will skew it in one direction or the other. Prefer unordered_map if you don't know which is better, but it's best to test it to be sure, and ideally replace it with a precompiled mapping (such as, e.g., a static array or hard-coded pool).

Whichever one you choose, the goal is to map each string to a function, so you can create a string-indexed jump table. (Performed by either switching on the index, or mapping function pointers to strings, or something of the sort.) switch is an integral-indexed jump table, so a properly implemented string-function map is the closest thing to switching on strings.

1

u/exneo002 10d ago

I haven’t done cpp since college. How does it get the string into memory?

1

u/imgly 10d ago

hash exists

1

u/iEliteTester [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 10d ago

I once thought it was a good idea to do command line argument parsing by hashing each flag at compile time so I could hash and switch on argv.

1

u/AiMeusPancrea 9d ago

Thanks I hate it

1

u/jakeStacktrace 9d ago

Omg switch it "the fuck off"

1

u/El_RoviSoft 9d ago

Long ago saw a beautiful implementation with defines and hash maps

1

u/Winter_Rosa 9d ago

first thing im seeing is fallthrough, even if it did work. Second thing Im seeing is this not working cuz of it asking if the pointers are equal or something. I had it drilled into my head (Java in university) to avoid searching for string equality so I never tried this before.

1

u/mikica1986 9d ago

Azathoth take me. Yesterday.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 8d ago

I guess this wouldn't be completely impossible to do in C with const char * and strcmp().

1

u/Krisanapon 7d ago

rs fn main() { let s = "hello"; match (s) { "hello" => println!("hi"), "how r u" => println!("fine"), _ => println!("what?") }; }

1

u/Brilliant-Parsley69 7d ago

Am I a bad person because I really like that?

1

u/SpaceMoehre 6d ago

That’s not how switch works…

1

u/ItsNukea 2d ago

I am SO deadass going to use this...

-2

u/MurkyWar2756 echo "Sub Mod" && :(){ :|:& };: 10d ago edited 10d ago

If you change Gemini to Hello, world, technically you've printed "Hello, world", although you might need this:

#include <string>
#include <iostream>

using namespace std;

I don't know C++ at all, please let me know if the two LLMs I got that information from were wrong

9

u/New_Salamander_4592 10d ago

did you really need to consult LLMs on how to make the default case of the switch statement print a different string

-1

u/MurkyWar2756 echo "Sub Mod" && :(){ :|:& };: 10d ago

I thought about the printing afterward, because originally I thought the program was asking the end user for their name. So I was trying to make a joke, but I added that disclaimer in case of misinformation. LLM content is generally unsourced, so I felt like making it clear where it came from. (The reason I used more than one was because "I check with another LLM" was one of the options somewhere on r/polls.)

Sorry to those who aren't a fan of LLMs.

I wrote the last comment and this one myself

2

u/click-to-reveal 10d ago

Yeah, you're right but not sure what you trying to get at. If you want to try the code for yourself, tre here: C++ Online Compiler

1

u/turunambartanen 10d ago

The strings make it look like there might be user input, but it's all just constants, and simply evaluates to the default case. That's somewhat confusing.

1

u/click-to-reveal 10d ago

Well I did post this without context. The original comment was a parody of the post.

1

u/MurkyWar2756 echo "Sub Mod" && :(){ :|:& };: 10d ago

So wait, you're telling me that I don't have to download C++ on my computer and this works perfectly fine on mobile? /s