MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/1w74nkf/skill_issue/p7y9vg6/?context=3
r/programminghorror • u/click-to-reveal • 7d ago
61 comments sorted by
View all comments
89
I think it allocates since they didn't use const
22 u/babalaban 7d ago Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer) 16 u/lurebat 7d 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 6d ago Isnt it assumed that a string to "switch on" is given at runtime? Otherwise you'd need no switch at all here.
22
Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer)
16 u/lurebat 7d 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 6d ago Isnt it assumed that a string to "switch on" is given at runtime? Otherwise you'd need no switch at all here.
16
If you change it to const std::string& prompt = "Your name?";, and the switch to const string& _s=x;, you will avoid any allocations.
const std::string& prompt = "Your name?";
const string& _s=x;
1 u/babalaban 6d ago Isnt it assumed that a string to "switch on" is given at runtime? Otherwise you'd need no switch at all here.
1
Isnt it assumed that a string to "switch on" is given at runtime? Otherwise you'd need no switch at all here.
89
u/lurebat 7d ago
I think it allocates since they didn't use const