|
Boost : |
From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-03-09 07:44:48
John Maddock wrote:
> >How about adding the following (surprisingly missing :) type
> transformation
> >templates to our 'type traits' library?
>
> Because they are not needed, adding a cv-qualifier, is
> *always* as simple
> as:
>
> typedef const T ct;
Ok, so 'ct_if' was unnecessary there.. still, however simple the
implementation of the 'add_const' template is, I would prefer the explicit
'add_const<T>::type' form to plain 'const T' in case where I want to
emphasize that when I wrote the code I was aware of the fact that 'T' may be
already 'const'-qualified, and that I considered the issues that may arise
with the (enclosing template's) code in such situation and found them to be
non-issues :).
IMO, the explicit form is just more... well, explicit in expressing the
behavior/intent of the code, and, after all, it just happened that, thanks
to the 7.1.5.1, the implementation of the 'add_const' is trivial. For
instance, it might be easy for one to explain to other people the usage of
the library on the examples like
BOOST_STATIC_ASSERT((is_same<add_pointer<remove_pointer<int>::type>::type,
int*>::value));
BOOST_STATIC_ASSERT((is_same<remove_pointer<add_pointer<int>::type >::type,
int>::value));
BOOST_STATIC_ASSERT((is_same<add_pointer<remove_pointer<int*>::type>::type,
int*>::value));
BOOST_STATIC_ASSERT((is_same<remove_pointer< add_pointer<int*>::type
>::type, int>::value));
BOOST_STATIC_ASSERT((is_same<add_const<remove_const<int>::type>::type, const
int>::value));
BOOST_STATIC_ASSERT((is_same<remove_const<add_const<int>::type>::type,
int>::value));
BOOST_STATIC_ASSERT((is_same<add_const<remove_const<const int>::type
>::type, const int>::value));
BOOST_STATIC_ASSERT((is_same<remove_const<add_const<const int>::type>::type,
int>::value));
without going into explanation why they can't actually write the last 4
lines using the former 4 as an example, why there are no 'add_const'
template in the library and the first 3 of those samples should be rewritten
by replacing 'add_const' with the plain 'const'-qualifier (which makes two
of them pointless), and why the fourth line cannot be written even in this
way, as the explicit redundant cv-qualifiers are prohibited, but this is not
important anyway as the library is aimed to help writing template code, and
the "add-plain-const" approach does work with typedefs and template type
arguments.. :)
Speaking seriously, I wouldn't expect every user of the 'boost::type_traits'
library to be aware of that particular rule (see, I wasn't :), and from a
user's point of view as far as one wrote 'add_const<T>::type' and it worked
as (s)he expected, (s)he don't care much about whether the implementation of
that template was trivial or not ;).
We won't loose anything if we add these templates, will we?
Aleksey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk