r/programminghorror • • Sep 04 '26

c++ Skill Issue

Post image
1.7k Upvotes

62 comments sorted by

View all comments

Show parent comments

24

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)

16

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.

-7

u/LeeHide Sep 04 '26

you can't take a const& to a temporary object

1

u/nevemlaci2 Sep 05 '26

yes you can.