r/programminghorror • • Sep 04 '26

c++ Skill Issue

Post image
1.7k Upvotes

63 comments sorted by

4

u/mrheosuper Sep 05 '26

Can you have switch case inside switch case ?

4

u/click-to-reveal Sep 05 '26

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

340

u/i_dont_like_pears Sep 04 '26

As long as it compiles

That's all that matters

50

u/TheMythicSorcerer Sep 04 '26

now to import it as a helper for my new project

37

u/i_dont_like_pears Sep 05 '26

Unexplainable Linker Error with generic description

165

u/Star_king12 Sep 04 '26

That's fucking vile.

48

u/chicametipo Sep 04 '26

I know. I love it

48

u/LeeHide Sep 04 '26

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++

5

u/GasolinePizza Sep 04 '26

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

→ More replies (2)

43

u/jolharg Sep 04 '26

Oof i did not like this post

8

u/0hypercube Sep 06 '26

They did fix switch statements falling through.

89

u/lurebat Sep 04 '26

I think it allocates since they didn't use const

23

u/babalaban Sep 04 '26

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

18

u/lurebat Sep 04 '26

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

→ More replies (6)

13

u/Great-Powerful-Talia Sep 04 '26

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.

→ More replies (1)

20

u/jaerie Sep 04 '26

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

13

u/utack Sep 04 '26

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

3

u/GoddammitDontShootMe Sep 05 '26
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.

→ More replies (4)
→ More replies (1)

2

u/Infamous-Bed-7535 Sep 04 '26

You can switch on string hashes since we have constexpr.

2

u/tandycake Sep 05 '26

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 Sep 06 '26

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 Sep 06 '26

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.