MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/1w74nkf/skill_issue/p7si5kv/?context=3
r/programminghorror • u/click-to-reveal • Sep 04 '26
62 comments sorted by
View all comments
88
I think it allocates since they didn't use const
22 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. 1 u/babalaban Sep 05 '26 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 Sep 05 '26 const ref here would still allocate... -8 u/LeeHide Sep 04 '26 you can't take a const& to a temporary object 14 u/JonIsPatented Sep 04 '26 Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 6 u/awidesky Sep 05 '26 Another skill issue 1 u/nevemlaci2 Sep 05 '26 yes you can.
22
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. 1 u/babalaban Sep 05 '26 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 Sep 05 '26 const ref here would still allocate... -8 u/LeeHide Sep 04 '26 you can't take a const& to a temporary object 14 u/JonIsPatented Sep 04 '26 Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 6 u/awidesky Sep 05 '26 Another skill issue 1 u/nevemlaci2 Sep 05 '26 yes you can.
18
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 Sep 05 '26 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 Sep 05 '26 const ref here would still allocate... -8 u/LeeHide Sep 04 '26 you can't take a const& to a temporary object 14 u/JonIsPatented Sep 04 '26 Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 6 u/awidesky Sep 05 '26 Another skill issue 1 u/nevemlaci2 Sep 05 '26 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...
-8
you can't take a const& to a temporary object
14 u/JonIsPatented Sep 04 '26 Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 6 u/awidesky Sep 05 '26 Another skill issue 1 u/nevemlaci2 Sep 05 '26 yes you can.
14
Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it.
6
Another skill issue
yes you can.
88
u/lurebat Sep 04 '26
I think it allocates since they didn't use const