Boost logo

Boost :

From: Jesse Jones (jesjones_at_[hidden])
Date: 2000-10-21 19:19:21


>
>> Copy(p, p+10, j);
>
>That does not call copy<int*const, int*>, it calls copy<int*, int*> (due to
>the way argument deduction works), if you explicitly instantiate
>copy<int*const, int*> then you should see a compile time error.

Right, there's one trivial error (is_POD needs a const int
specialization) and a more serious one where I try to get the value_type
using iterator_traits (because int* const is not a valid iterator type).

>Further if that is not a compile time error then the function prototype
>should be declared:
>
>template <typename InputIter, typename ForwardIter>
>inline ForwardIter Copy(const InputIter first, const InputIter last,
>ForwardIter result);
>
>IMO, instantiating template functions with cv-qualified types is even more
>of a mistake than instantiating classes with cv-qualified types, there are
>three options:
>
>1) the code does not compile (this is the most likely case with
>const-qualified types)

Which is reasonable.

>2) the code compiles but produces identical code to the non-cv-qualified
>case. Note that this can only occur if the template arguments are
>explicitly provided, it can never occur via template argument deduction.
>In this case as a matter of good manners and to avoid unnecessary code
>bloat, the cv-qualifier should be stripped from the type before being
>passed to the template argument list.

Sure, but this is something client's would have to do and damn few will
know enough not to explicitly instantiate functions using cv-qualified
types.

>3) The code compiles but produces incorrect code, think about:
>
>copy<int*, int*volatile>
>
>should this work?

Yes. :-) Why wouldn't it? You're explicitly telling the compiler to load
the pointer whenever it has to access it and save it whenever it is
modified. It seems to me that the compiler can do this easily enough with
the element by element version and with the memcpy version.

  -- Jesse


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