Refactoring Traits and Policies (in Containers)

M

Milburn Young

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>::eek:ther::const_pointer )'. How are traits
reflected in policies if they are only introduced to the container?
When would my refactoring not work?

Milburn Young
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top