Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Return by const value? GotW #6

7 views
Skip to first unread message

Kevin Goodsell

unread,
Mar 19, 2003, 10:54:25 PM3/19/03
to
Guru of the Week #6 (http://www.gotw.ca/gotw/006.htm) talks about
making non-built-in return types const (when returning by value).
Sutter recommends using const, but mentions that Lakos recommends not
using const. I don't understand Lakos's reasons, though. Quoted from
the link above:

"Lakos (pg. 618) argues against returning const value, and notes that
it is redundant for builtins anyway (for example, returning "const
int"), which he notes may interfere with template instantiation."

Can someone explain what this means? Specifically, why would it
interfere with template instantiation?

Also, I'd like to hear your thoughts on which method to use.

Thanks.

-Kevin

Alf P. Steinbach

unread,
Mar 19, 2003, 11:54:21 PM3/19/03
to

One down-to-earth practical reason for not using const on a return
value is that using const prohibits certain advanced optimizations
such as "mojo", <URL: http://www.moderncppdesign.com/mojo/>.

More generally, it needlessly restricts the client code, e.g. from
calling non-const methods on the result, possibly template instantiation
(on the function, not the dynamic result, but I haven't really thought
about that or investigated the matter, so I cannot give details), etc.

Restrictions are good when they serve some useful purpose, e.g. to
promote abstraction, bad when they're used only for dogmatic reasons.


Hth.,

- Alf

0 new messages