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>: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
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>: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