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

Re: didactics of reference parameters[Supersedes]

69 views
Skip to first unread message

Mr Flibble

unread,
Mar 16, 2016, 12:38:17 AM3/16/16
to
On 16/03/2016 03:19, Stefan Ram wrote:
> Supersedes: <references-2...@ram.dialup.fu-berlin.de>
> [& --> const &]
>
> Say, someone has never heard of »references«, but now he
> needs to call a function
>
> void f( ::std::string const & s );
>
> . He already knows how to call
>
> void f( ::std::string s );
>
> . What does he need to learn about reference parameters,
> when he wants to learn just enough to be able to /call/
> pre-defined functions with reference parameters, but does
> not need to be able to write such functions himself?
>
> (I want to write a stripped-down version of my introduction
> to references, that contains just what one needs to be able
> to /call/ such functions, not to /write/ them. What do I
> need to teach about references in this case, so that errors
> in the client's code are avoided?)

Your habit of Typing ::std::foo instead of std::foo is both really
annoying and absurd.

/Flibble


Juha Nieminen

unread,
Mar 16, 2016, 5:27:22 AM3/16/16
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> Say, someone has never heard of »references«, but now he
> needs to call a function
>
> void f( ::std::string const & s );
>
> . He already knows how to call
>
> void f( ::std::string s );
>
> . What does he need to learn about reference parameters,
> when he wants to learn just enough to be able to /call/
> pre-defined functions with reference parameters, but does
> not need to be able to write such functions himself?

I'm trying to think of any way that calling a function that takes an object
by value is different from calling a function that takes the object by
const reference, but even if there is some situation where it's different
(from the caller's perspective), I'm drawing a blank.

I suppose that if the object itself cannot be copied, then it makes a
difference, but with std::string there is no such problem. That's the only
thing I can think of.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Reinhardt Behm

unread,
Mar 16, 2016, 9:03:21 AM3/16/16
to
If the object is large copying it will be a performance bottleneck and if it
is really large the copy for call by value will not be possible because of
not enough memory.

--
Reinhardt

Jerry Stuckle

unread,
Mar 16, 2016, 9:04:00 AM3/16/16
to
If the called function needs a mutable object (but doesn't want to
change the original), it can either take a copy or make a copy of a
constant reference. Either way, it needs to make a copy. But if it
doesn't require a mutable copy, using a constant reference is obviously
more efficient.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

Gareth Owen

unread,
Mar 16, 2016, 1:41:27 PM3/16/16
to
Juha Nieminen <nos...@thanks.invalid> writes:

> I'm trying to think of any way that calling a function that takes an object
> by value is different from calling a function that takes the object by
> const reference, but even if there is some situation where it's different
> (from the caller's perspective), I'm drawing a blank.

Anything "mutable" will be one case...

Alternatively:

std::string s("I'm a global\n");

bool is_global_s(const std::string& str)
{
return (&s == &str);
}

..vs..

bool is_global_s(const std::string str)
{
return (&s == &str);
}

woodb...@gmail.com

unread,
Mar 16, 2016, 2:22:20 PM3/16/16
to
I don't do that to be annoying. I do it because it's
the responsible thing to do.

Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

Juha Nieminen

unread,
Mar 17, 2016, 6:49:41 AM3/17/16
to
But the original poster didn't ask how it behaves differently. He asked
what is needed to call pre-defined functions.

I can't think of a single situation where calling a function taking
a std::string by value would be different from calling a function
taking a std::string by const reference.

Ian Collins

unread,
Mar 20, 2016, 7:14:12 PM3/20/16
to
No it isn't. No sane developer would create their own top level
namespace called "std".

--
Ian Collins

woodb...@gmail.com

unread,
Mar 20, 2016, 7:53:30 PM3/20/16
to
Someone might do it by accident, or out of ignorance or malice.

Brian
Ebenezer Enterprises - An ounce of prevention is worth
more than a pound of cure.

http://webEbenezer.net

Öö Tiib

unread,
Mar 20, 2016, 8:16:52 PM3/20/16
to
On Monday, 21 March 2016 01:53:30 UTC+2, woodb...@gmail.com wrote:
> On Sunday, March 20, 2016 at 6:14:12 PM UTC-5, Ian Collins wrote:
> > On 03/17/16 07:22, woodb...@gmail.com wrote:
> > > On Tuesday, March 15, 2016 at 11:38:17 PM UTC-5, Mr Flibble wrote:
> > >> Your habit of Typing ::std::foo instead of std::foo is both really
> > >> annoying and absurd.
> > >>
> > >> /Flibble
> > >
> > > I don't do that to be annoying. I do it because it's
> > > the responsible thing to do.
> >
> > No it isn't. No sane developer would create their own top level
> > namespace called "std".
> >
>
> Someone might do it by accident, or out of ignorance or malice.

We typically want fastest feedback possible about such ignorance,
accidents and/or malice taking part in our code-bases. Or don't we?

Ian Collins

unread,
Mar 20, 2016, 8:24:04 PM3/20/16
to
On 03/21/16 12:53, woodb...@gmail.com wrote:
> On Sunday, March 20, 2016 at 6:14:12 PM UTC-5, Ian Collins wrote:
>> On 03/17/16 07:22, woodb...@gmail.com wrote:
>>> On Tuesday, March 15, 2016 at 11:38:17 PM UTC-5, Mr Flibble wrote:
>>>> Your habit of Typing ::std::foo instead of std::foo is both really
>>>> annoying and absurd.
>>>>
>>>> /Flibble
>>>
>>> I don't do that to be annoying. I do it because it's
>>> the responsible thing to do.
>>
>> No it isn't. No sane developer would create their own top level
>> namespace called "std".
>>
>
> Someone might do it by accident, or out of ignorance or malice.

The former is unlikely and would be quickly spotted, the later could
apply to anything such as a nasty #define.

Neither justify cluttering code with unnecessary clutter.

--
Ian Collins

woodb...@gmail.com

unread,
Mar 20, 2016, 8:52:56 PM3/20/16
to
I guess so, but I don't really understand what you are
saying.

Jerry Stuckle

unread,
Mar 20, 2016, 10:59:22 PM3/20/16
to
Remember who you're talking about, Ian... :)

Öö Tiib

unread,
Mar 20, 2016, 11:07:13 PM3/20/16
to
I meant that fastest feed-back are IDE underlying things red or compiler
errors about conflicts. The '::std' cooperates with potential ignorance,
accidents and/or malice by avoiding such compiler errors. People see
that it also clutters the code in process with needless colons.
Something is controversial about "most responsible thing to do" here.

woodb...@gmail.com

unread,
Mar 21, 2016, 11:10:40 AM3/21/16
to
On Sunday, March 20, 2016 at 10:07:13 PM UTC-5, Öö Tiib wrote:
> On Monday, 21 March 2016 02:52:56 UTC+2, woodb...@gmail.com wrote:
> > On Sunday, March 20, 2016 at 7:16:52 PM UTC-5, Öö Tiib wrote:
> > > On Monday, 21 March 2016 01:53:30 UTC+2, woodb...@gmail.com wrote:
> > > >
> > > > Someone might do it by accident, or out of ignorance or malice.
> > >
> > > We typically want fastest feedback possible about such ignorance,
> > > accidents and/or malice taking part in our code-bases. Or don't we?
> >
> > I guess so, but I don't really understand what you are
> > saying.
>
> I meant that fastest feed-back are IDE underlying things red or compiler
> errors about conflicts. The '::std' cooperates with potential ignorance,
> accidents and/or malice by avoiding such compiler errors.


I agree that detecting potential problems is helpful, but
I'd rather know that the software will work right for
users in their environment.

Brian
Ebenezer Enterprises
http://webEbenezer.net

Stuart Redmann

unread,
Mar 24, 2016, 11:04:05 AM3/24/16
to
Stefan Ram wrote:
[snip]
> (I want to write a stripped-down version of my introduction
> to references, that contains just what one needs to be able
> to /call/ such functions, not to /write/ them. What do I
> need to teach about references in this case, so that errors
> in the client's code are avoided?)


Since this is an educational problem it may be worth mentioning that some
programming languages that aim to be a successor of C have been designed in
such a way that the user has to use a different syntax for passing by
reference. For example in C# you have to use the keyword "ref" in the
parameter list.

Regards,
Stuart



0 new messages