Traits in STL

N

Narinder

Hello

I am somewhat curious about the reason for the way types in the STL
are dependant on trait classes. For example the template class
basic_string is defined as :

template<class charT,
class basic_string;

but not as:

template<class charT,
class basic_string
{
typedef traits_t<charT> traits;
typedef Allocator_t<charT> Allocator;
....


You would think (or perhaps not) that the second approach is less
error prone since it would be less likely that your traits would be
inconsistent with the char type. The only reason I can think of is
that you may have a traits class specifically for char which wouldn't
make sense for any other types and thus wouldn't need to be a
template. In that case you could always write it as template class and
use traits to activate it only for char types.

Regards
N
 
I

Ian Collins

Hello

I am somewhat curious about the reason for the way types in the STL
are dependant on trait classes. For example the template class
basic_string is defined as :

template<class charT,
class traits = char_traits<charT>,
class Allocator = allocator<charT> >
>
class basic_string;

but not as:

template<class charT,
template<class> class traits_t = char_traits,
template<class> class Allocator_t = allocator>
>
class basic_string
{
typedef traits_t<charT> traits;
typedef Allocator_t<charT> Allocator;
....


You would think (or perhaps not) that the second approach is less
error prone since it would be less likely that your traits would be
inconsistent with the char type. The only reason I can think of is
that you may have a traits class specifically for char which wouldn't
make sense for any other types and thus wouldn't need to be a
template. In that case you could always write it as template class and
use traits to activate it only for char types.

Another reason could be a lot of compilers didn't support template
template parameters when the standard was published (as an aside, I
don't think template template parameters are used anywhere in the
current standard library). Not being able to support std:;string would
have been a bit of a problem!
 
N

Narinder

Another reason could be a lot of compilers didn't support template
template parameters when the standard was published (as an aside, I
don't think template template parameters are used anywhere in the
current standard library).  Not being able to support std:;string would
have been a bit of a problem!

hmmm ... there something quite disturbing about that. Surely the
implementation of the compilers should depend on the standard not the
other way round ...
 
I

Ian Collins

hmmm ... there something quite disturbing about that. Surely the
implementation of the compilers should depend on the standard not the
other way round ...

Imagine where we'd be if the standard library required export....
 
N

Narinder

Imagine where we'd be if the standard library required export....

Mr Comeau would be very wealthy.
I could be wrong, but wasn't export introduced after the work on the
STL already along way on ?
 
Q

Qi

You would think (or perhaps not) that the second approach is less
error prone since it would be less likely that your traits would be
inconsistent with the char type. The only reason I can think of is
that you may have a traits class specifically for char which wouldn't
make sense for any other types and thus wouldn't need to be a
template. In that case you could always write it as template class and
use traits to activate it only for char types.

Don't know the real reason.

But IMO, template template parameter is quite less flexible than class.

In your second case, you limit that the traits must be a template with
only one parameter, which is too restrict.

The user may want to provide another char_traits, which first parameter
is charT, second is another accessorial parameter for internal use.
 
I

Ian Collins

Please don't quote signatures.
Mr Comeau would be very wealthy.
:)

I could be wrong, but wasn't export introduced after the work on the
STL already along way on ?

There's another reason: a large part of the library was existing
practice and pre-standard compilers didn't have template template
parameters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top