This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Lazaro Caruso wrote:Hi,
I would like to know what is the difference between returning reference and const reference?
The difference between a reference and a const reference is merely that you can modify the referenced value (or call non-const member functions on it) when you have a plain reference, but can only call const member functions on an object for which you have a const reference, and you cannot modify it if it is a built-in type.
Lazaro Caruso wrote:
Is fun2 faster or is it the same?
There is absolutely no speed difference in either case.
Hi. I know I've covered all the uses of "const" quite definitively in one or more of my books... time for me to go look up my answers! <grin>
But yes -- I don't disagree with anything Anthony AJ said. Most obviously, there is absolutely no difference in execution speed whatsoever. "const" only concerns what the compiler permits and what it will not permit.
But the speed angle itself is very interesting. One of the purposes of "const" is to permit passing a reference rather than value (which can be much, much faster in some cases) while still maintaining one of the benefits of passing by value -- preventing changes to the argument, which might be what you want in some cases. Passing an argument as a "const" reference is therefore very fast (usually, it is just an address that is put on the stack) but it accomplishes the same thing as pass by value, in that it protects the original data from being altered.
BUT, I understand the question is: what about "const" functions? Off the top of my head, I must echo what Anthony AJ said, but I'll put it in my own words... it seems to me that "const" functions are relevant when we are talking about member functions of some class. When such a function is called, it is bared from making changes to the object (that is, the object through which it is called); in fact, I don't think it can make any changes to class members at all.
What is the purposes of such a member function? Well, imagine you have declared a const object. You may want to be able to call safe member functions that cannot change the object. That's what a const function is... it is a function that has agreed not to change the object itself. Therefore, the compiler permits you to call a member function of a const object only if that function is also declared "const".
Such a "const" member function might do things such as report back value of one or more class members, without making changes to them. Or, a const member function can alter any amount of data so long as it doesn't alter class members.
What is less clear to me is what is the point of a const function if it is not a member function of some class. I may have to do some research on that.
Thanks for the question,
Anthony Aj Williams
Joined: Jun 10, 2011
Brian Overland wrote:What is less clear to me is what is the point of a const function if it is not a member function of some class. I may have to do some research on that.
You cannot have a const function that is not a member of a class, though in C++11 you can have a constexpr function that is a non-member. If the arguments to a constexpr function are compile-time constants then the entire function is evaluated at compile time, and the result is also a compile-time constant, which than therefore be used as a template non-type parameter, or the size of an array. Variables of POD types (such as int or simple classes with no constructors) with static storage duration (globals, namespace-scope variables and local statics) that are initialized with a compile-time constant are statically initialized before any code is run. This can avoid order-of-initialization problems, and of course gives the application a performance boost as less code has to be evaluated at runtime.
In order to enable this compile-time evaluation, functions declared to be constexpr must satisfy additional constraints, such as no modifications to variables, no dynamic_casts, no control flow statements except a single return statement --- basically the function body and parameter and return types can only use types and operations that can occur in a constant expression.