Channels ▼


Living By the Rules: Part II

Handy Extensions

One of the first things you learn when you're using containers is to write a bunch of typedefs so that you can change the type of the container without rewriting the rest of the code:

typedef std::vector<int> values;
typedef values::iterator iter;
typedef values::const_iterator const_iter;

Now you can write a loop easily:

values data;
for (iter it = data.begin(); it != data.end(); ++it)
   { /* ... */ }

And you can change the container from, say, a vector to a deque by changing its typedef:

typedef std::deque<int> values;

The typedefs for iter and const_iter will change meaning appropriately, and the loop will compile correctly. But that's a lot of boilerplate to have to write every time you use a container type. Soon, you'll be able to write that code like this:

typedef std::vector<int> values;
for (auto it = data.begin(); 
        it != data.end(); ++it)
   { /* ... */ }

and when you change the container type, the compiler will simply change the type to match [6].

If you write code that uses dynamic libraries (DLLs under Windows, and shared libraries under UNIX), you've probably used a language extension that lets you talk about a template instantiation whose code lives somewhere else. For example, std::basic_string<char> should be instantiated in the dynamic library that has the rest of the Standard Library code, and not in the code that uses it. Unfortunately, if you just use the name of the template instantiation, the compiler doesn't know that you don't mean to put the code there:

std::basic_string<char> string; 
  // might generate code in 
  // executable

The trick that Standard Library implementers have been using relies on a compiler extension. The header <string> generally has a declaration for basic_string<char> that looks something like this:

extern template <> basic_string<char>;  
   // not instantiated here

Then somewhere in the library's implementation code, there's the actual instantiation:

template <> basic_string<char>;
   // may be instantiated here

That is, putting the extern keyword in front of the template declaration tells the compiler that you're using a template instantiation, but you don't want to have it instantiated at that point. That extension is now part of the language, giving you better control over where templates are instantiated.

New Algorithms

The algorithms [7] min, max, min_element, and max_element are sometimes used in pairs, when you're concerned about both the minimum and the maximum values. They've been supplemented by two new algorithms, minmax and minmax_element, that determine both the minimum and the maximum value with a single call to the algorithm. When you're searching through a sequence this is obviously beneficial, because you only have to go through the sequence once instead of making two passes—once to find the minimum value and once to find the maximum value.

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.