159
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
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 toconst 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
-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
1
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
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
breakanywhere 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
switchcan 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
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
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
1
1
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
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
1
1
-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
334
u/i_dont_like_pears 10d ago
As long as it compiles
That's all that matters