4

I am very new to C++ smart pointers and I am having a hard time understanding this advice around using them for function arguments.

"C++ Coding Standards: 101 Rules, Guidelines, and Best Practices " says that these are the reasons to pass in smart pointer.

Prefer passing by (smart) pointer

  • if the argument is optional (so callers can pass null as a “not available” or “don’t care” value)
  • or if the function stores a copy of the pointer
  • or otherwise manipulates ownership of the argument.

Can somebody please give me examples of each one of them and why not using a smart pointer would be a bad idea in the cases?

unj2
  • 52,135
  • 87
  • 247
  • 375
  • 5
    What C++ coding standard is that? – Robᵩ Oct 01 '12 at 20:27
  • There is a book by Sutter and I believe Alexandrescu simply called "C++ Coding Standards". I think that's what the OP is quoting but I can't seem to find my own copy to verify. It is a VERY good book that should be on every serious C++ devs. bookshelf. – Edward Strange Oct 01 '12 at 20:42
  • @Rob : Added the full name of the book – unj2 Oct 01 '12 at 20:56
  • Found my copy. The text matches word for word but the bulleted list is different. C++ Coding Standards, Sutter and Alexandrescu, Item 25, pg 46. http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586 – Edward Strange Oct 01 '12 at 20:57
  • @CrazyEddie yes, it's in the [list of recommended books on C++](http://stackoverflow.com/q/388242/1025391) here. – moooeeeep Oct 01 '12 at 21:38

4 Answers4

7

Prefer passing by (smart) pointer [when ...]

I think you are misreading the coding standard. You read this as "these are the reasons to use a smart pointer". What the author intended is "these are the reasons to use some kind of pointer, which might optionally be a smart pointer."

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Can you please explain the last point of why pointers are preferred over references by giving an example? – unj2 Oct 01 '12 at 21:13
4

Prefer passing by (smart) pointer

Note how "smart" is in parenthesis?

What the person is talking about here is passing by pointer vs. reference. Smart is there in parenthesis because you'd follow similar rules when working in a team that prefers smart pointers.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • That makes sense. Can you explain the last point of why pointers are preferred over references by giving an example? – unj2 Oct 01 '12 at 21:04
2

I am going to assume that this question relates to pointers in general, as that is what the quoted text in the question is explaining.

There are several reasons why you would want to use a smart pointer (or good old fashioned pointers in general). The first and most obvious one is that when you pass something by reference or by pointer, you pass the pointer to the item in memory, which means you get the actual object, instead of a copy, as you get when passing by value. This is useful when you want to manipulate an object using some function, or simply to reduce copying (imagine sending a large text file as a value all the time, that sure would be inefficient!)

Next, the opportunity of passing something as a null value. This basically means that the parameter can be passed in as a "does not exist" which in turn can be handled in logic. For instance, if a file pointer is null, a new file is created.

For smart pointers specifically: Smart pointers are pointers that have additional management algorithms going on behind the scenes, this could be reference counting or other options. For instance, you can use a unique_pointer to make sure that only one pointer to the object exists at any given time. For more information, see wikipedia http://en.wikipedia.org/wiki/Smart_pointer

If indeed the question is related to the workings of general pointers, please see also this introduction http://www.tech-recipes.com/rx/1232/c-pointers-pass-by-value-pass-by-reference/

Marius Brendmoe
  • 365
  • 1
  • 9
  • 2
    I generally tend to believe there's usually a better way to deal with "null" conditions. For example, the one you talk about should probably have a proxy object of some sort that simply opens the file if it's not. Client code then doesn't need to do any checking. – Edward Strange Oct 01 '12 at 20:46
  • While I agree with your addition, the nature of the question suggested a lower proficiency in programming principles, and thus a more simplistic response was presented. – Marius Brendmoe Oct 01 '12 at 20:49
1

Smart pointers exist for one, and exactly one, function:

ownership of the argument

Smart pointers enforce this so that you don't cock it up, which is highly likely since manually managing ownership is ridiculously error-prone, whereas smart pointers guarantee.

There is no other justification for using a smart pointer over a regular pointer.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Going to have to agree, I almost wrote the exact same thing (re: moron). +1 – Ed S. Oct 01 '12 at 20:31
  • 1
    After some thought, I think @Crazy Eddie is right. It should be taken as; *"Prefer passing a pointer (possibly a smart pointer) [as opposed to a reference] when..."* A reference to the guide would have been nice as it would have given us some context. – Ed S. Oct 01 '12 at 20:37
  • I believe that "moron" is Herb Sutter. – Edward Strange Oct 01 '12 at 20:37
  • Which completely doesn't prevent him from having been a moron at the time of writing. – Puppy Oct 01 '12 at 22:09