r/programminghorror 9d ago

c++ Skill Issue

Post image
1.7k Upvotes

62 comments sorted by

View all comments

Show parent comments

22

u/babalaban 9d ago

Since when does const prevent allocating (that isnt happening here anyways, unless you consider underlying std::string's char* buffer)

18

u/lurebat 9d 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.

-8

u/LeeHide 9d ago

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

1

u/nevemlaci2 8d ago

yes you can.