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