Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2002-03-09 12:23:33


On Saturday, March 9, 2002, at 12:04 PM, rwgk wrote:

> Would you have a code sample that could help me avoid
> learning about potential pitfalls the hard way?

What? You didn't like:

template <class F : !is_convertible<F, value_type>::value>
vector(size_type n, F init, const Allocator& = Allocator());

:-)

The "do-the-right-thing clause" refers to 23.1.1 paragraphs 9-11.
You're in good company Ralf. This type of bug bit the best of them just
before the standard was ratified.

A typical implementation might look something like (warning, unchecked
pseudo code):

template <class F>
vector(size_type n, F init, const Allocator& a = Allocator())
{
        choose_F_init(n, init, a, int2type<!is_convertible<F,
value_type>::value>());
}

Then you've got two overloads for choose_F_init:

1. Takes a int2type<false>. F *is* convertible to value_type. This is
a mistaken binding. Redirect to the code that implements
vector(size_type, value_type, allocator); (converting init to a
value_type of course).

2. Takes a int2type<true>. F will not convert to value_type. This is
the real deal, do whatever you were going to do in the first place with
(n, init, a).

Now isn't:

template <class F : !is_convertible<F, value_type>::value>
vector(size_type n, F init, const Allocator& = Allocator());

*much* easier?! :-) The compiler does the redirecting for you, but
under your precise instructions.

-Howard


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk