Connecting Tech Pros Worldwide Forums | Help | Site Map

How Best to Incorporate Traits and a Policies

Milburn Young
Guest
 
Posts: n/a
#1: Jul 18 '07
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! ]


Carl Barron
Guest
 
Posts: n/a
#2: Jul 19 '07

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! ]

James Kanze
Guest
 
Posts: n/a
#3: Jul 19 '07

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! ]

Closed Thread