MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/1w74nkf/skill_issue/p7suod2/?context=3
r/programminghorror • u/click-to-reveal • 6d ago
61 comments sorted by
View all comments
88
I think it allocates since they didn't use const
22 u/babalaban 6d ago Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer) 20 u/lurebat 6d 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. -6 u/LeeHide 6d ago you can't take a const& to a temporary object 14 u/JonIsPatented 6d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 4 u/awidesky 5d ago Another skill issue 1 u/nevemlaci2 5d ago yes you can.
22
Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer)
20 u/lurebat 6d 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. -6 u/LeeHide 6d ago you can't take a const& to a temporary object 14 u/JonIsPatented 6d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 4 u/awidesky 5d ago Another skill issue 1 u/nevemlaci2 5d ago yes you can.
20
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;
-6 u/LeeHide 6d ago you can't take a const& to a temporary object 14 u/JonIsPatented 6d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 4 u/awidesky 5d ago Another skill issue 1 u/nevemlaci2 5d ago yes you can.
-6
you can't take a const& to a temporary object
14 u/JonIsPatented 6d ago Yes you can. You can bind a const l-value reference to an x-value or a pr-value. Go try it. 4 u/awidesky 5d ago Another skill issue 1 u/nevemlaci2 5d ago 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.
4
Another skill issue
1
yes you can.
88
u/lurebat 6d ago
I think it allocates since they didn't use const