Connecting Tech Pros Worldwide Help | Site Map

How Best to Incorporate Traits and a Policies

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 01:05 PM
Milburn Young
Guest
 
Posts: n/a
Default How Best to Incorporate Traits and a Policies

I see the STL using class templates accepting a traits and a policy.
To me, the traits are the "what" and the policy is the "how". How can
a policy know what to do without the traits of the topic? Wouldn't it
be better for containers to take a policy template argument and for
the policy to take a traits template argument? Consider an allocator
(policy) that is going to be used in a multi-threaded environment.
The programmer could use something like the following (simplified)
traits.

template<typename TYPE>
struct NormalTraits
{
typedef TYPE value_type;
typedef value_type *pointer;
typedef std::size_t size_type;
};
template<>
struct NormalTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef value_type *pointer;
typedef std::size_t size_type;
};

template<typename TYPE>
struct VolatileTraits
{
typedef TYPE value_type;
typedef volatile value_type *pointer;
typedef std::size_t size_type;
};
template<>
struct VolatileTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef volatile value_type *pointer;
typedef std::size_t size_type;
};

Now, a programmer could specify the VolatileTraits<to an allocator
policy for a multi-threaded container and the policy would know what
types should be given to the signatures of methods like 'pointer
allocate( size_type, typename
TRAITS::rebind<void>::other::const_pointer )'.

Milburn Young


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


  #2  
Old July 19th, 2007, 03:05 PM
Carl Barron
Guest
 
Posts: n/a
Default Re: How Best to Incorporate Traits and a Policies

In article <1184740202.232783.131560@z24g2000prh.googlegroups .com>,
Milburn Young <Milburn.Young@gmail.comwrote:
Quote:
template<>
struct NormalTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef value_type *pointer;
typedef std::size_t size_type;
};
>
template<>
struct VolatileTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef volatile value_type *pointer;
typedef std::size_t size_type;
};
>
Now, a programmer could specify the VolatileTraits<to an allocator
policy for a multi-threaded container and the policy would know what
types should be given to the signatures of methods like 'pointer
allocate( size_type, typename
TRAITS::rebind<void>::other::const_pointer )'.
>
seems that you have no traits publically accessable if type is void:)

boost and tr1 provide add_volatile and is_volatile templates
which are simple to write if you don't have either.

template <class A,class Bool = is_volatile<A class Allocator;

template<class Aclass Allocator<A,true_type>
{
// volatile allocator
};

template <class Aclass Allocator<A,false_type>
{
// non volatile allocator
};
less is more:)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

  #3  
Old July 19th, 2007, 08:25 PM
James Kanze
Guest
 
Posts: n/a
Default Re: How Best to Incorporate Traits and a Policies

On Jul 18, 3:59 pm, Milburn Young <Milburn.Yo...@gmail.comwrote:
Quote:
I see the STL using class templates accepting a traits and a policy.
To me, the traits are the "what" and the policy is the "how".
Any distinction is really arbitrary. In the STL, they are
called traits. In other, more recent literature, they are
called policy. Same thing in the end.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.