Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Don't pass by reference to non-const?

Reply
Thread Tools

Don't pass by reference to non-const?

 
 
Sousuke
Guest
Posts: n/a
 
      05-01-2010
I'm having doubts about the validity of the following guideline:

http://google-styleguide.googlecode....ence_Arguments

For some reason I haven't noticed whether this is generally followed.
Is it? Or is it just something made up at Google? What do you think of
it?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-01-2010
On 05/ 2/10 09:30 AM, Sousuke wrote:
> I'm having doubts about the validity of the following guideline:
>
> http://google-styleguide.googlecode....ence_Arguments
>
> For some reason I haven't noticed whether this is generally followed.
> Is it? Or is it just something made up at Google? What do you think of
> it?


I haven't seen it used and I don't like it. It appears inconsistent,
mixing reference and pointer parameters for no good reason.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Sousuke
Guest
Posts: n/a
 
      05-01-2010
On May 1, 5:10*pm, "Daniel T." <danie...@earthlink.net> wrote:
> Sousuke <s0s...@gmail.com> wrote:
> > I'm having doubts about the validity of the following guideline:

>
> >http://google-styleguide.googlecode....ide.xml?showon...
> > ce_Arguments#Reference_Arguments

>
> > For some reason I haven't noticed whether this is generally followed.
> > Is it? Or is it just something made up at Google? What do you think of
> > it?

>
> Stroustrup advocates this guideline as well in "The C++ Programming
> Language." I don't particularly care for it myself.


Thanks. Could you tell me the section?

> My own guideline is to use pointers as arguments if the object pointed
> to has to live longer than the function being called (or if the argument
> is a C array.) For example, if the function is storing the object
> pointed to somewhere for later use.


Or if the pointed-to type is an abstract class, I think.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-01-2010
* Sousuke:
> I'm having doubts about the validity of the following guideline:
>
> http://google-styleguide.googlecode....ence_Arguments
>
> For some reason I haven't noticed whether this is generally followed.
> Is it? Or is it just something made up at Google? What do you think of
> it?


Please quote the guideline you refer to.

<quote>
Reference Arguments

All parameters passed by reference must be labeled const.

Definition: In C, if a function needs to modify a variable, the parameter must
use a pointer, eg int foo(int *pval). In C++, the function can alternatively
declare a reference parameter: int foo(int &val).

Pros: Defining a parameter as reference avoids ugly code like (*pval)++.
Necessary for some applications like copy constructors. Makes it clear, unlike
with pointers, that NULL is not a possible value.

Cons: References can be confusing, as they have value syntax but pointer semantics.

Decision:

Within function parameter lists all references must be const:

void Foo(const string &in, string *out);

In fact it is a very strong convention in Google code that input arguments are
values or const references while output arguments are pointers. Input parameters
may be const pointers, but we never allow non-const reference parameters.

One case when you might want an input parameter to be a const pointer is if you
want to emphasize that the argument is not copied, so it must exist for the
lifetime of the object; it is usually best to document this in comments as well.
STL adapters such as bind2nd and mem_fun do not permit reference parameters, so
you must declare functions with pointer parameters in these cases, too.
</quote>


I.e. Google would design std::getline with pointer argument.

It sounds political.

From a non-political perspective it's pretty stupid since it requires extra
notation and requires unnecessary bug-vectors, both something you would
absolutely not want in the code, but presumably that "decision" made sense in
some workplace politics, something with not too disturbing consequences that
some group was very opposed to but had to accept; you're the boss, man.


Cheers,

- Alf (telepathic circuit engaged)
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-01-2010
On 05/ 2/10 10:10 AM, Daniel T. wrote:
> Sousuke<> wrote:
>
>> I'm having doubts about the validity of the following guideline:
>>
>> http://google-styleguide.googlecode....howone=Referen
>> ce_Arguments#Reference_Arguments
>>
>> For some reason I haven't noticed whether this is generally followed.
>> Is it? Or is it just something made up at Google? What do you think of
>> it?

>
> Stroustrup advocates this guideline as well in "The C++ Programming
> Language." I don't particularly care for it myself.
>
> My own guideline is to use pointers as arguments if the object pointed
> to has to live longer than the function being called (or if the argument
> is a C array.) For example, if the function is storing the object
> pointed to somewhere for later use.


Um, surely any object that gets passed by non-const reference lives
longer the the function being called?

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-01-2010
On 05/ 2/10 11:05 AM, Sousuke wrote:
> On May 1, 5:10 pm, "Daniel T."<danie...@earthlink.net> wrote:
>
>> My own guideline is to use pointers as arguments if the object pointed
>> to has to live longer than the function being called (or if the argument
>> is a C array.) For example, if the function is storing the object
>> pointed to somewhere for later use.

>
> Or if the pointed-to type is an abstract class, I think.


Why?

--
Ian Collins
 
Reply With Quote
 
Sousuke
Guest
Posts: n/a
 
      05-02-2010
On May 1, 6:19*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 05/ 2/10 11:05 AM, Sousuke wrote:
>
> > On May 1, 5:10 pm, "Daniel T."<danie...@earthlink.net> *wrote:

>
> >> My own guideline is to use pointers as arguments if the object pointed
> >> to has to live longer than the function being called (or if the argument
> >> is a C array.) For example, if the function is storing the object
> >> pointed to somewhere for later use.

>
> > Or if the pointed-to type is an abstract class, I think.

>
> Why?


Maybe I'm just confused because the code I'm currently working on
deals heavily with reference-counted pointers to abstract classes (so
they're always dynamically allocated), so it makes sense to pass them
around as pointers.
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      05-02-2010

"Sousuke" <>
> I'm having doubts about the validity of the following guideline:
>
> http://google-styleguide.googlecode....ence_Arguments
>
> For some reason I haven't noticed whether this is generally followed.
> Is it? Or is it just something made up at Google? What do you think of
> it?


As a general point, stay away from google's style guide. It has many
questionable or outright WTF points. (At least it did a year ago, when I
twitlisted it.)

Sure you can find a set of really good guides. When in doubt look at
Sutter/Alexandrescu book and its references.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-02-2010
On May 1, 10:42 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 05/ 2/10 09:30 AM, Sousuke wrote:


> > I'm having doubts about the validity of the following guideline:


> >http://google-styleguide.googlecode....ide.xml?showon...


> > For some reason I haven't noticed whether this is generally
> > followed. Is it? Or is it just something made up at Google?
> > What do you think of it?


> I haven't seen it used and I don't like it. It appears
> inconsistent, mixing reference and pointer parameters for no
> good reason.


Yes and no. The logic is that in arguments are references, out
and inout pointers. This has two advantages:

-- It requires explicit action at the call site in order to
pass an out argument; you can see immediately that the
argument might change.

-- It ensures that even if you currently compile with a
compiler that doesn't enforce the rule forbidding
initializing a non-const reference with an rvalue (VC++, for
example), your code will compile with more "correct"
compilers: all compilers require an lvalue in order to use
the (built-in) & operator.

Neither are "killer" arguments, and certainly a lot of other
conventions are possible. In the end, you only have two
choices, and there are far more than one binary information that
might be useful to indicate. Still, way, way back, this was the
convention I preferred. I'd gotten out of it, because the
places I'd worked in used other conventions, but the circle has
turned, and where I work now uses it (and used it before I got
there).

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-02-2010
On May 1, 11:10 pm, "Daniel T." <danie...@earthlink.net> wrote:
> Sousuke <s0s...@gmail.com> wrote:
> > I'm having doubts about the validity of the following guideline:


> >http://google-styleguide.googlecode....ide.xml?showon...
> > ce_Arguments#Reference_Arguments


> > For some reason I haven't noticed whether this is generally
> > followed. Is it? Or is it just something made up at Google?
> > What do you think of it?


> Stroustrup advocates this guideline as well in "The C++
> Programming Language." I don't particularly care for it
> myself.


> My own guideline is to use pointers as arguments if the object
> pointed to has to live longer than the function being called
> (or if the argument is a C array.) For example, if the
> function is storing the object pointed to somewhere for later
> use.


That's another convention. Probably the most common. (At
least, it seemed to be when I did a small survey of the issue.
About 15 years ago.) A third is to use a pointer if a null
pointer is conceivably valid, a reference otherwise.

> I use const reference as an optimization of passing by
> value.


The issue here isn't const reference as an optimization of pass
by value. It's non-const referenced vs. non-const pointer for
out (and inout) parameters.

Note that regardless of the convention otherwise: if the <op>=
(and prefixed ++ and --) operators aren't members, then their
first (only) argument must be a non-const reference. (And
although I generally make them members, there are also good
arguments for a convention which makes them free functions.)

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
Re: Do you usually to pass a reference to set (or other stlcontainer) as const puzzlecracker C++ 5 08-22-2008 08:03 AM
Problem understanding pass by value and pass by reference of arrays and what happens in the memory venkatagmail C++ 11 10-03-2007 02:00 PM
How to let C++ compiler raise a warning or error when a const is assigned to a const reference. PengYu.UT@gmail.com C++ 14 09-17-2006 11:08 PM
const class reference return from function, what does const do? Jim Langston C++ 2 05-11-2006 09:49 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57