logoalt Hacker News

cowlevel • today at 7:52 AM • 1 reply • view on HN

If you make it a non-const reference the compiler won't let you pass a result of another function call such as sum(get_daily_prices())


Replies

aw1621107 • today at 8:17 AM

Sure, but I think that has less to do with optimizations and more to do with avoiding potential footguns. The standard could, in principle, allow temporaries to bind to non-const references (and IIRC older versions of Visual Studio did do such a thing).

And even without such a change, such a restriction is pretty easily worked around without a const reference with something like T&& temp = get_daily_prices(); sum(temp);. I don't think that'll result in different codegen than either passing a temporary to a const ref or allowing binding a temporary to a non-const ref.