Topic: Seeking type algebra idiom
Author: vaps4bm@prism.gatech.edu (Brian McNamara!)
Date: 2000/10/25 Raw View
"Trevor L. Jackson, III" <fullmoon@aspi.net> once said:
>Partial template specialization in conjunction with function overloading
>seems to handle some cases but it requires a fairly awkward set of
>implementation classes. If possible I'd like to locate an idiom that
>can be used within a single class or class hierarchy.
I'm not sure I understand what you want, but what about
template <class T> struct RelatedType {};
template <> struct RelatedType<int> { typedef unsigned int type; };
template <> struct RelatedType<long> { typedef char type; };
template <class TPrime, class T>
void f_helper( T ) {
std::cout << "In f_helper<" << typeid(TPrime).name() << ","
<< typeid(T).name() << ">" << std::endl;
// ...
}
template <class T>
void f( T x ) {
typedef typename RelatedType<T>::type RT;
f_helper< RT >( x );
}
int main() {
int x=3;
long y=3;
f(x);
f(y);
}
In general, it is straightforward to make type computers which map types
to types at compile-time using the idiom with RelatedType above. We use
the idiom in FC++ a lot, for example.
http://www.cc.gatech.edu/~yannis/fc++/
The general form is
typename TypeComputer<InputType1,InputTypeN>::OutputTypeK
and you simply create specializations (or nested classes) which
implement the right typedef-logic.
(If this is the kind of thing that you want, then it's just another
idiom, and we should probably move further conversation to
comp.lang.c++.moderated or the like.)
--
Brian McNamara
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Trevor L. Jackson, III" <fullmoon@aspi.net>
Date: 2000/10/20 Raw View
Given the flexibility of standard templates I suspect it is possible to
construct type maps or links such that one can derive related types
without explicit coding at the application level. Examples of the
mapping include the signed/unsigned version of an unsigned/signed type
or the least integer that can hold a full mantissa value of various
float formats (void if none).
The goal is to have some template User< typename T > that uses a related
type T' internally. I suspect it would not be hard to create this kind
of interface on top of RTTI, but I've successfully avoided RTTI so far,
and would like to preserve my virginity so to speak.
Partial template specialization in conjunction with function overloading
seems to handle some cases but it requires a fairly awkward set of
implementation classes. If possible I'd like to locate an idiom that
can be used within a single class or class hierarchy.
This seems to work:
// Users want to call this:
template< typename User_t >
struct Visible {
static void method( User_t const & usr );
};
// Actual implementation here
template< typename User_t, typename Related_t >
struct Hidden {
static void method( User_t const & usr, Related_t const & rel );
};
// Ancillary information here
template< typename Related_t >
struct Internal {
static Related_t rel_;
};
// Specializations of the user-visible class
void Visible< one_t >::method( one_t const & arg )
{
static Internal< rel_one_t > const datum = { 1 };
Hidden< one_t, rel_one_t >::method( arg, datum );
}
void Visible< two_t >::method( two_t const & arg )
{
static Internal<rel_two_t > const datum = { 2 };
Hidden< two_t, rel_two_t >::method( arg, datum );
}
void userfunc( void )
{
Visible< int >::method( -10 );
Visible< long >::method( 100 );
}
But as an idiom I can only describe it as yuck. Is there a simpler,
more elegant idiom known?
If C++ does not support this kind of type agility, then we are far from
tapping the full potential of the language. Is there reason to hope we
might reach it at the next revision?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]