MESSAGE
DATE | 2015-09-07 |
FROM | Ruben Safir
|
SUBJECT | Subject: [LIU Comp Sci] C++ Fwd: Re: References to const
|
From owner-learn-outgoing-at-mrbrklyn.com Mon Sep 7 13:48:51 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id D44C716117A; Mon, 7 Sep 2015 13:48:51 -0400 (EDT) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id C778B16117F; Mon, 7 Sep 2015 13:48:51 -0400 (EDT) Delivered-To: learn-at-nylxs.com Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by mrbrklyn.com (Postfix) with ESMTP id 1C61216117A for ; Mon, 7 Sep 2015 13:48:27 -0400 (EDT) Received: from [10.0.0.19] (www.mrbrklyn.com [96.57.23.82]) by mailbackend.panix.com (Postfix) with ESMTPSA id 6008317481 for ; Mon, 7 Sep 2015 13:48:27 -0400 (EDT) Message-ID: <55EDCDEB.6000601-at-panix.com> Date: Mon, 07 Sep 2015 13:48:27 -0400 From: Ruben Safir User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: learn-at-nylxs.com Subject: [LIU Comp Sci] C++ Fwd: Re: References to const References: In-Reply-To: X-Forwarded-Message-Id: Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Sender: owner-learn-at-mrbrklyn.com Precedence: bulk Reply-To: learn-at-mrbrklyn.com
-------- Forwarded Message -------- Subject: Re: References to const Date: Wed, 29 Jul 2015 20:41:48 CST From: Richard Reply-To: Richard Organization: multi-cellular, biological Newsgroups: comp.lang.c++.moderated References:
[Please do not mail me a copy of your followup]
Robert spake the secret code thusly:
>C++ Primer page 61 "..we can initialize a reference to const from any >expression that can be converted to the type of the reference. In >particular, we can bind a reference to const to a nonconst object, a >literal, or a more general expression." > >What is the reason for this exception?
It means that you can have a function or method that accepts a parameter by const reference and call that function or method with an instance of a non-const object.
This is really the only way that it makes sense to use const references; otherwise the only thing you could associate with a const reference would be a const object and most objects change at *some* point in the program.
Consider the following function:
bool ends_with_foo(const std::string &text) { return text.size() >= 3U && text.substr(text.size()-3) == "foo"; }
This function is saying that it's parameter 'text' is obtained by a const reference to std::string. The const means we promise not to modify the argument inside the function -- which is good because this function is acting like a predicate and it shouldn't be changing the object under inspection.
If we could only call 'ends_with_foo' on const strings, this would be unfortunate, because we'd end up doing needless copies just to be able to call this predicate. The rule that allows a const reference to be initialized from a non-const object makes the predicate useful in the obvious way:
std::string ender{"I end with foo"}; std::string unender{"I end with bar"}; bool yes = ends_with_foo(ender); bool no = ends_with_foo(unender);
Otherwise I'd have to get things into a constant string just to be able to query it:
std::string ender{"I end with foo"}; const std::string const_ender{ender}; std::string unender{"I end with bar"}; const std::string const_unender{unender}; bool yes = ends_with_foo(const_ender); bool no = ends_with_foo(const_unender);
-- "The Direct3D Graphics Pipeline" free book The Computer Graphics Museum The Terminals Wiki Legalize Adulthood! (my blog)
[ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
|