Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-04-02 10:58:50


From: "Mark Rodgers" <mark.rodgers_at_[hidden]>
>
> Well I think in terms of code, and it really is quite simple IMHO:
>
> Traits look like this:
>
> template< typename T >
> class SomeTraits
> {
> // Stuff that tells us about T
> };
>
> and Policies look like this
>
> template< typename Policy >
> class SomeThing
> {
> // Delegates some behaviour to Policy
> };
>
> IOW, traits are class templates and policies are template parameters so
> there really can't be any confusion between the two. So in the case

I think that misses an important traits trait 8^) that I didn't
communicate well: for a given namespace/library, there is only
one traits class for a given set of attributes. You can create
other traits that collect different information, collect the same
information with a different interface, but for a given interface
and set of information, there's only one traits class.

> std::basic_string
>
> template<class charT, class traits = char_traits<charT>,
> class Allocator = allocator<charT> >
> class basic_string {
>
> "char_traits" is a traits class template but "traits" is actually a policy
> template parameter. It is quite reasonable for specialisations of traits
> templates to be used as arguments to policy parameters, which is exactly

That's reasonable only if the traits class provides the semantics
expected of the policy which is, obviously, the case in your
basic_string example.

> what happens when we write
>
> std::basic_string< char,std::char_traits<char> >

I agree.

> But I don't think there is anything in basic_string that requires the
> template argument to be a specialisation of a class template. I think it
> would be quite legitimate to write
>
> class MyFunkyCharBehaviour { /*...*/ };
> typedef std::basic_string<char,MyFunkyCharBehaviour> MyFunkyString;
>
> so the "traits" parameter is indeed misnamed.

Right.

> However std::char_traits is most definitely a traits template and doesn't
> *have* to be used as a policy. In fact std::basic_string could have been
> written to exclusively use char_traits *instead* of a policy.

Right.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

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