Reputation: 8325
I've written a simplified version of the STL basic_string class :
template<typename chrT>
class strT
{
protected:
chrT * m_pcBuf;
size_t m_nLen;
public:
strT( void );
strT( const chrT * pcStr );
strT( const strT<chrT> & rsStr );
virtual ~strT( void );
virtual inline size_t len( void ) const;
virtual int cmp( const strT<chrT> & rsStr ) const;
virtual inline const chrT & operator [] ( size_t nPos ) const;
virtual inline chrT & operator [] ( size_t nPos );
virtual bool & operator == ( const strT<chrT> & rsStr ) const;
virtual bool & operator != ( const strT<chrT> & rsStr ) const;
virtual bool & operator < ( const strT<chrT> & rsStr ) const;
virtual bool & operator > ( const strT<chrT> & rsStr ) const;
virtual bool & operator <= ( const strT<chrT> & rsStr ) const;
virtual bool & operator >= ( const strT<chrT> & rsStr ) const;
virtual inline strT<chrT> & operator = ( const strT<chrT> & rsStr );
virtual inline operator chrT * ( void );
protected:
void _alloc( size_t nLen );
void _realloc( size_t nLen );
public:
static const size_t none;
};
template<typename inchr, typename outchr>
strT<outchr> convert( const strT<inchr> & rsIn );
typedef strT<char> str;
typedef strT<wchar_t> wstr;
Currently, if i want to convert between, say, char and wchar_t, i can simply do :
int main( void )
{
str as = "foo";
wstr ws = convert<char, wchar_t>( as );
}
But instead of this functionnal approach i would like to have a template method allowing to do such conversions. This is very likely false, but here is what i'd like to do :
template<typename chrT>
class strT
{
// ...
public:
template<typename outchr>
virtual strT<outchr> convert( void ) const;
// ...
}
template<typename chrT, typename outchr>
strT<outchr> strT<chrT>::convert<outchr>( void ) const
{
// ...
}
And then :
int main( void )
{
str as = "foo";
wstr ws = as.convert<wchar_t>();
}
Is it possible please ?
Thanks for your help !
PS : Forgot to mention that i don't want to use C++11 features.
Upvotes: 1
Views: 136
Reputation: 78934
Why not just add a templated constructor?
template<typename chrT>
class strT
{
public:
template<typename otherT>
strT(otherT* o) { ... }
}
To implement the conversion, you can specialize the constructor for the different possibilities of chrT
and otherT
Upvotes: 1
Reputation: 171127
It's almost possible - you can have a member function template, but it cannot be virtual
. So if that's fine with you, drop virtual
from the declaration and you're set.
To see why member function templates can't be virtual
, think about the most common implementation of virtual member functions - the virtual function table. That's a table of pointers to functions, storing one pointer for each virtual function. How many pointers would need to be stored for a virtual member function template? One for convert<char>
, one for convert<wchar_t>
, one for convert<int>
, one for convert<int*>
, one for convert<int**>
, one for convert<std::vector<int> >
, one for ...
The problem is that arbitrarily many functions can be instantiated from the template. There's no way to do dynamic dispatch for them. (Note that a similar problem appears in all other dynamic dispatch implementations as well - a template is just potentially infinitely many functions)
Upvotes: 1