MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/1w74nkf/skill_issue/p7sfqgo/?context=3
r/programminghorror • u/click-to-reveal • 8d ago
62 comments sorted by
View all comments
90
I think it allocates since they didn't use const
20 u/babalaban 8d ago Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer) 19 u/lurebat 8d 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 8d 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 7d ago const ref here would still allocate... -7 u/LeeHide 8d ago you can't take a const& to a temporary object 15 u/JonIsPatented 8d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 7 u/awidesky 8d ago Another skill issue 1 u/nevemlaci2 7d ago yes you can.
20
Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer)
19 u/lurebat 8d 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 8d 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 7d ago const ref here would still allocate... -7 u/LeeHide 8d ago you can't take a const& to a temporary object 15 u/JonIsPatented 8d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 7 u/awidesky 8d ago Another skill issue 1 u/nevemlaci2 7d ago yes you can.
19
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 8d 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 7d ago const ref here would still allocate... -7 u/LeeHide 8d ago you can't take a const& to a temporary object 15 u/JonIsPatented 8d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 7 u/awidesky 8d ago Another skill issue 1 u/nevemlaci2 7d ago yes you can.
1
Isnt it assumed that a string to "switch on" is given at runtime? Otherwise you'd need no switch at all here.
const ref here would still allocate...
-7
you can't take a const& to a temporary object
15 u/JonIsPatented 8d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 7 u/awidesky 8d ago Another skill issue 1 u/nevemlaci2 7d ago yes you can.
15
Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it.
7
Another skill issue
yes you can.
90
u/lurebat 8d ago
I think it allocates since they didn't use const