Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2003-05-28 01:20:04


Hi,

I have been reading the discussiong here about adding the support for unicode.

Maybe there is a solution which would allow to have templated versions of
relevant classes AND static library in one box.

If we decide that specialization for char and wchar_t is sufficient, the headers
can contain generic template based declarations, but the implementation will be
given only for particular cases ( char and wchar_t ) and so it can be included in
the static lib.

Similar idea is used in stl for streams, strings and locales.

An Example:

aclass.hpp -----------------

template< typename T >
class AClass
{
...

        // Declaration of a member function
        bool dosomething( std::basic_string<T> );

...
};

aclass.cpp -----------------

bool AClass<char>::dosomething( string )
{
        return false;
}

bool AClass<wchar_t>::dosomething( wstring )
{
        return true;
}

-----------------------------------

Because definition of the member function in the .cpp are not templated,
they can reside in the static library.

With a help from preprocessor, it should be easy to define two variants
in one place.
Also that could a possibility, that the .cpp file will be included as headers
all this can be done using macros.

Example 2:

aclass.inl -------------------

TEMPLATE_DEF
bool AClass<TEMPLATE_T>::dosomething( std::basic_string<TEMPLATE_T> )
{
        return false;
}

aclass.cpp ------------------

#define TEMPLATE_T char
#define TEMPLATE_DEF
#include "aclass.inl"
#undef TEMPLATE_T
#undef TEMPLATE_DEF

#define TEMPLATE_T wchar_t
#define TEMPLATE_DEF
#include "aclass.inl"
#undef TEMPLATE_T
#undef TEMPLATE_DEF

aclass.hpp -----------------

template<T>
class AClass
{
...
};

...

#ifdef LIB_INLINE
#define TEMPLATE_T T
#define TEMPLATE_DEF template< typename T >
#include "aclass.inl"
#undef TEMPLATE_T
#undef TEMPLATE_DEF

-----------------------------

This way everybody can be satisfied with a reasonable effort.

Pavol


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