|
Boost : |
From: Daryle Walker (darylew_at_[hidden])
Date: 2006-11-12 15:42:28
On 11/11/06 9:10 PM, "Sascha Krissler" <boost-dev_at_[hidden]>
wrote:
> i had this little idea.
> when yo want to customize a class with a container like
>
> template <typename T, typename Container>
> struct Foo;
>
> then you must instanciate it with:
>
> Foo<int, std::vector<int> >
>
> the second int is redundant.
[TRUNCATE failed solution with a template-based template parameter and a
better solution with MPL reading or writing template parameters]
No, it's not redundant. STL stuff involving containers is set up so a
direct, non-template, class can be used as a container. Solutions involving
certain template games will break that philosophy.
I use asserts to make sure a container is compliant with what I need. Here
is an example from some code I'm putting in the Sandbox:
//========================================================================
#include <memory> // for std::allocator
#include <boost/type_traits/is_same.hpp> // for boost::is_same
#include <boost/static_assert.hpp> // for BOOST_STATIC_ASSERT
namespace boost { namespace math {
//...
template < int Radix, class Allocator = ::std::allocator<int> >
class big_radix_whole
{
// Pre-conditions
// (SNIP constraints on "Radix")
BOOST_STATIC_ASSERT( (is_same<int,
typename Allocator::value_type>::value) );
// ...
};
//...
} }
//========================================================================
Now I can use a non-template allocator class like:
//========================================================================
#include <cstddef>
class my_int_allocator // Note: _no_ "template" preamble!
{
public:
typedef int value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef int * pointer;
typedef int const * const_pointer;
typedef int & reference;
typedef int const & const_reference;
// You're screwed if you need "rebind".
// Put address, max_size, allocate, deallocate, etc. here...
};
//========================================================================
-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk