|
Boost : |
From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-01-05 08:28:15
I've took a rough look at the new version of the library; going to do a more
detailed review/test later, so now just a few
comments/questions/suggestions.
C: Splitting the library and the tests was one of the things I wanted to
propose/ask for just a few days ago, so I was glad to see it implemented
before I even asked ;). Actually, I was thinking about splitting it even
more further, almost a header for a template, but now I am not sure if it
makes much sense. Does anybody else have a strong opinion?
Q: it seems that there are no 'add_pointer' template in the
'transform_traits.hpp' header; is it intentional?
Q: 'same_traits.hpp' header seems to include the 'composite_traits.hpp'
header even if it does not really need it (in case of BOOST_MSVC being
defined); is it possible to avoid this somehow?
S: It seems that indeed there are no straightforward ways to implement
'remove_[const, volatile, cv, pointer, reference]' (and some other) type
traits templates on compilers without partial specialization, but:
a) some of such compilers are going to be around for a while;
b) many of us have to use such compilers at work;
c) lack of these templates is often one of the limiting factors in
implementing/porting generic libraries on/to these compilers/platforms,
so IMO in the meantime (while the compilers are catching up...) it is worthy
to consider introducing into the library some kind of "brute force
implementation" of these templates, which can provide poor users of such
compilers more-or-less working solution.
Below is a proposal of such implementation, and implementation of the
proposal (NPI :) can be seen at
http://www.egroups.com/files/boost/type_traits_proposal/type_traits/type_tra
its_spec.hpp:
For the compilers on which it is not possible to implement 'remove_[const,
volatile, cv, pointer, reference]' type traits templates due to their
(compilers) lack of support of class templates' partial specialization, the
'type_traits' library provides:
1) the explicit specializations of all 'remove_*' class templates for all
fundamental types and all their 1st and 2nd rank (cv-[un]qualified)
derivative pointer types; e.g. for 'remove_const' template it should be
something like this:
template<> struct remove_const<char const> { typedef char type; };
template<> struct remove_const<char const volatile> { typedef char volatile
type; };
template<> struct remove_const<char* const> { typedef char* type; };
template<> struct remove_const<char* const volatile> { typedef char*
volatile type; };
template<> struct remove_const<char const* const> { typedef char const*
type; };
template<> struct remove_const<char const* const volatile> { typedef char
const* volatile type; };
// so on...
Surprisingly, although at first sight this might seem as a tremendous amount
of work, a set of simple preprocessor macros can help to fold the
specializations of ALL 'remove_*' class templates for ALL fundamental types
into ~50 lines of code.
2) a user-level macro that will define the described above explicit
specializations for any user-defined type T, so after writing something
like:
struct my {};
BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(my)
the following code will work:
BOOST_STATIC_ASSERT((is_same<my, remove_reference<my&>::type>::value));
BOOST_STATIC_ASSERT((is_same<my, remove_const<my const>::type>::value));
// etc.
One potential problem with this implementation is that here there are quite
a number of standalone explicit specializations instead of just only one
partial, and now we really have to test all these combinations of types,
pointers and cv-qualifiers in order to be sure that these traits work
properly in all cases. It wasn't hard to write such test, though, applying
the same algorithm as in 'type_traits_spec.hpp', but in inverse order and
using templates instead of macros, and here are a few examples of these
tests - http://www.egroups.com/files/boost/type_traits_proposal/. If you do
actually run these tests you will see the total amount of _distinct_ test
cases executed - 525 for the 'remove_[const, volatile, cv]' traits test and
189 for the 'remove_reference' test, so these tests are in fact much more
comprehensive than "traditional" ones.
That's all for now :). Comments?
--Aleksey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk