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

C/C++

Double Plus Good?


Verity Stob Board of Technical Examinations

Modern C++

Answer as many questions as you can in the insufficient time. Marks will be awarded for neat handwriting and not whimpering.

Q1. An easy one to get you going. Compare and contrast your method of code indenting and laying out braces with two heretical approaches.

 

Q2. As exhorted by the Supreme Guruhood, you have started using the Standard Library containers in your code. One day your compiler makes you a present of the following error message:

error C2440: 'initializing' : cannot convert from 'class std::_Tree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::_Kfn,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::const_iterator' to 'class std::_Tree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::_Kfn,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::iterator'

No constructor could take the source type, or constructor overload resolution was ambiguous

Which is the correct response of a working C++ programmer?

A. Carefully study the error message, breaking it down into its component parts. By this method, it is possible to obtain a picture of what is going on, and so track down the problem.

B. Write a Perl or Python program to parse the compiler's output, factoring out high-level typedefs. For example, expressions involving basic_string can often be simplified to String. When this process is complete, an easily readable error message will appear.

C. Jump in and delete a whole bunch of const qualifiers from everywhere near the error. That usually works, if only you zap enough of them.

D. Burst into tears. Retreat to the loo, howling: "I wish I had been an estate agent, like my sister. She's got her own Freelander 4x4, three children and a timeshare beach apartment in Le Touquet."

 

Q3. Describe a use of overloaded unary logical or bitwise operators that will not, on rereading the code a few months later, reveal you to yourself as a pretentious show off.

 

Q4. On page 127 of his book Exceptional C++, Herb Sutter writes:

"... 'using namespace std;' ... dumps all the names in std into the current namespace and thus eliminates much of the advantage of having namespaces in the first place" [My emphasis — VS]

whereas recently in comp.lang.c++.moderated, this heinous fragment of Scott Meyers' code was presented:

using namespace std; // so sue me

causing a near riot, panicky moderators closing down the thread with CS gas, etc.

What should a C++ head do in this bewildering maelstrom of guru opinion?

A. I know you, Verity. I bet there's much more to it than you imply, and as a conscientious programmer I will check out the (slightly edited, and unfortunately omitting the passage quoted above) web version of Herb Sutter's Exceptional C++ article and the original thread that includes contributions from known suspects such as Herb Sutter (stating his revised view), Andrei Alexandrescu, Francis Glassborrow et al.

B. It's a medical condition. When I see the words "Koenig Lookup," my vision goes all blurry. A pity, but there it is.

C. Yup, let's sue Scott Meyers. I never trusted him after he was quoted in the C++ slag-off section of The UNIX-HATERS Handbook. And he was against printf(), the traitor! Besides, I need to type "std::" so that the editor can pop up the little list of suggestions of what to do next.

D. Sorry, what are namespaces again? Are they a bit like locales?

 

Q5. Rank the following "good practice" tips according to how much they jar the eye:

A. When comparing variables with constants, write the constant first to avoid accidental assignment ie write

if (22 == a)

not

if (a == 22)

B. When incrementing something, use the pre-increment rather than post-increment to avoid creating unnecessary temporaries ie write

++c;

not

c++;

C. When writing a for loop, don't write a for loop. Instead use the std::for_each() algorithm, and make a piddling little function to encapsulate the one line of code that you want to put in it.

D. When casting, use static_cast<> and dynamic_cast<> instead of dear old (void *). Yes, I know I should have got over this by now, but I haven't, ok?

 

Q6. Smart pointers, eh? Blimey. Who would have thought it?

 

Q7. Discuss the role of exception specifications in your working code.

Q7A. Yes you do remember. It's that bit that's supposed to go after a function's parameters. To quote the primary source:

void f() throw (x2, x3);

See?

 

Q8. While we are on exceptions, which of the following most closely encapsulates your attitude to exception handling in C++?

A. Don't fight it. When you've gotta go, you've gotta go.

B. For some reason we tend to get most of our exceptions in destructors — so frankly there isn't much point.

C. By dint of plentiful try...catch constructs throughout our code base, we are sometimes able to prevent our applications from aborting. We think of the resultant state as "nailing the corpse in the upright position".

D. It's so great that setjmp()/longjmp() is still there for us.

 

Q9. At this point we were going to have one of those little snippets of code so beloved of C++ quiz setters, and I was going to ask you whether it compiled. Then it was pointed out to me that this was going on the Web, so there was nothing to stop you cutting and pasting it into a file, and just trying it out to see if it did compile.

Yes, you would have. It's no good sitting there looking all innocent, as though a template template parameter wouldn't melt in your mouth.

 

Q10. In the small hours of the night, and the lonely watches before dawn, are you ever troubled by the worrying feeling that the whole C++ business has just got too complex? And that you are losing your grip?

Well, are you?


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.