Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Conversations:Contracts, Promises, and Mere Semantics


October 2002 C++ Experts Forum/Conversations


I began that day as I began most workdays — sitting in my cube with fresh coffee at my elbow, catching up on the morning's email before turning to pound out some new code. Oddly enough, it wasn't a day that the Guru suddenly appeared behind me at an opportune time... instead, it was a day I overheard it happening to someone else.

I was sitting at my desk in my cube, working quietly, and hearing the faint keyclicks of nearby coworkers working invisibly in their cubes, when the voice came.

"The time has come," the Guru said, "to speak of many things."

I jumped, mostly out of habit I think. I looked around, but the Guru wasn't standing behind me. I kept listening and heard a chair squeak in the cubicle next to mine — it was Kerry [1], our group's new bright but geeky intern, jumping in his seat. She had to be with him. I wondered how long he'd last; the Guru was known to scare away most interns in the first few months, before their probation period could finish. Kerry was already showing the early signs of cracking. I figured he'd last another few weeks.

I eavesdropped, with only slight feelings of guilt:

"Bob was showing me a problem with my new code..." I heard Kerry begin gamely.

"Indeed, my child?" I could imagine her eyebrow arching and smiled to myself.

"Uh... yeah. It's a coding standards thing, I'll work it out myself..."

I heard the snap of a book shutting. The Guru would have closed the tome she was carrying. I wondered idly which tome she was carrying today. "Tell me, young one," I overheard the Guru say to Kerry, "what is it that our coding standards say? Pray tell."

"Uh... they're right here." As Kerry's fingers clicked on the keys, I quietly pulled up the same document on my screen and searched to the right spot as I heard him recite. It was this portion:

    When declaring parameter types, prefer to follow the traditional C++ interface idioms:

    • If the argument will be changed, pass it by pointer.
    • If the argument will not be changed, pass it by value.

"I didn't do it that way, so Bob pointed it out. That's all. It's okay. It's okay; I'll change it; really I will," Kerry finished, sounding nervous.

There was a pause, and I wondered whether the Guru was smiling or frowning. Finally, she said: "Let his words stand... for now. Update the code, but do not check it in. Instead, give Bob the updated version for his review and his own local use, and wait."

"Wait? ... For what?" Kerry's voice asked.

This time I was sure the Guru was smiling. "Only wait, my apprentice," she said, and I heard the rustle of paper and receding footsteps.

Show's over, I thought, and went back to work. But the best was yet to come....


A few hours later, I was still pounding away in my cubicle when I heard Kerry get called to Bob's desk, which was a little further away, but not far enough away that I didn't hear Bob chewing poor Kerry right out. After some more of that, which Bob quite seemed to enjoy, Kerry was eventually released and forlornly (I imagined) returned to his cubicle beside mine. His chair creaked as he sat down, and all was quiet again ... but not for very long.

Barely five minutes later, there was another Guru voice, another squeak of Kerry's chair (or was it Kerry? he did have a tendency to emit squeaks when the Guru appeared), another dialogue:

"Of why the C is boiling hot," the Guru announced suddenly, "and whether pigs have wings." I smiled at the last, realizing that she might mean Bob.

(squeak) "I... I spoke to Bob..."

"Indeed you did, my child, I could hear it across the building," came the Guru's soft voice. There was another snap of a quickly closed tome. "And what did he say?"

"That my code must be wrong; it broke his code," Kerry stammered. "But I don't know what I've done wrong. I did just what he said! I did just what the coding standards said, too!"

"Ah," — I imagined that here the Guru smiled sharply — "but the two are the same thing. For Bob is the one who wrote that part of the coding standard."

"... He... He is?"

"Oh, yes. He most certainly is, my apprentice. That is why the coding standards are wrong."

"Wrong?"

"Wrong for idiomatic C++, that is," she added. "They were mostly correct for idiomatic C, which did not have references."

"But... if they're wrong, then why weren't they updated?"

"Our new manager, Pete Williams, listens overmuch to Bob and does not see the need," she sighed. "Nor did his predecessor. Still, the coding standards are not that incorrect. The idiomatic C++ would be more like this..." I heard the squeak of a marker on the whiteboard, and here is what I saw on that whiteboard later:

    When declaring parameter types, prefer to follow the traditional C++ interface idioms unless it is inappropriate:

    • If the argument will be changed, pass it by pointer or reference to non-const.
    • If the argument will not be changed, pass it by reference to const, or if it is cheap to copy, by value.

"This statement is closer to the truth," the Guru concluded, "but, my apprentice, you must note the words 'prefer' and 'unless'. This merely documents common idiom. It is not dogma. It is not without exceptions."

"Uh... yeah. That's right. Bob said that it works with everything he tried except for..."

"auto_ptr objects," the Guru nodded, finishing for him, and I heard the marker resume squeaking as she continued writing on the whiteboard.

Kerry made a gurgled astonished sound. "How did you know that! Did Bob tell you? Did you see?"

"I merely know. I need not see." She continued writing on the board, and here is what I saw a few minutes later:

template<typename T>
void Mutate( T* byPointer );

template<typename T>
void Mutate( T& byReference );

template<typename T>
void LeaveAlone( const T* byPointerToConst );

template<typename T>
void LeaveAlone( const T& byReferenceToConst );

template<typename T>
void LeaveAlone( T byValue );

"Meditate on this parable, and then answer: For which types T are the function names true? Apprentice!" she called.

I didn't know she meant me until I heard a different kind of snap, and a rubber band bounced off my head. Then I scrambled up quickly and joined them, rubbing my scalp. "Huh? What?" I muttered. "I was busy working."

"Indeed you were working your ears." The Guru narrowed her eyes for a moment, feigning annoyance, and then let me see a twinkle in her eye. Putting down the whiteboard marker, she reopened the thick tome I now saw her carrying, tucked a gray lock behind one ear, smiled beatifically, and began to walk away. "My apprentice will explain, my child..." her parting words wafted back over her shoulder just before she turned the corner and disappeared.

I shook my head, grinning. What a character! But Kerry looked oddly at me, and I wonder if maybe he wasn't starting to put me in the same "odd bloke" category as the Guru. I discovered that I didn't mind if he did, which was an interesting revelation to me.

"Young one," I said, and smiled, and smiled more widely still once I realized what I'd just said, "what happens when you pass an auto_ptr?"

"Uh... oh!" Clearly Kerry's penny had dropped. "You transfer ownership! Is that what she meant?"

"Yup," I agreed. "You've got it. And the same would be true of any class that transferred ownership — one way to spot them, by the way, is by the non-const reference parameter in their copy constructor. So here's your answer: pass-by-value normally means you're leaving the source object alone, except that for auto_ptrs it means exactly the opposite — and worse, because you're not just changing the source auto_ptr, but resetting it to null. And pass-by-pointer usually means you're planning to change the source object, except that for auto_ptrs it means exactly the opposite — that you're avoiding changing it. For auto_ptr, the traditional idioms work exactly backwards, and that's why code that assumes the usual idioms, such as containers assume for their contained objects, does not work with auto_ptrs. But it's not just auto_ptrs; it's the case for any class with unusual transfer-of-ownership copy semantics. Always watch for that non-const reference parameter in the copy constructor; that's your dead giveaway that something unusual is going on.

"And now," I said to Kerry, walking away and letting my voice drift away behind me, "I must go to resume my meditations." I reached the end of the hallway and turned the corner just in time to get out of his view before I couldn't hold it inside any more and succumbed to a sudden fit of silent giggles.

References

[1] H. Sutter and J. Hyslop. "Conversations: A Midsummer Night's Madness," C/C++ Users Journal C++ Experts Forum, August 2002, <www.cuj.com/experts/2008/hyslop.htm>.

Jim Hyslop is a senior software designer at Leitch Technology International Inc. He can be reached at [email protected].

Herb Sutter (<www.gotw.ca>) is secretary of the ISO/ANSI C++ standards committee, author of the acclaimed books Exceptional C++ and More Exceptional C++, and one of the instructors of The C++ Seminar (<www.gotw.ca/cpp_seminar>). In addition to his independent writing and consulting, he is also C++ community liaison for Microsoft.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.