|
Boost : |
From: John Maddock (John_Maddock_at_[hidden])
Date: 2000-10-21 06:17:35
> 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.
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)
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.
3) The code compiles but produces incorrect code, think about:
copy<int*, int*volatile>
should this work?
I contend that there is no way it can give the correct behaviour, unless we
can guarentee that none of the pointers passed to copy DO NOT EXHIBIT
VOLATILE BEHAVIOUR. In which case the non-volatile equivalent should be
used.
- John.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk