Re: template specialization
* Konstantin:
mqrk wrote:
The simplest (and probably the best) solution is like this:
The problem still remains:
template <typename T> struct Container< T, std::set >
{
std::set<T> data;
};
template <typename T> struct Container< T, std::list >
{
std::list<T> data;
};
These are specializations, and one needs to write a primary template.
However, std::set template has three parameters: set<Key, Compare,
Alloc>, and std::list has only two: list<Type, Alloc>
Thus, if the primary template is
template < typename, template < typename, typename, typename > class >
class Container;
then the second specialization yields an error ("error C3201: the
template parameter list for class template 'std::list' does not match
the template parameter list for template parameter ...")
and if the primary template is
template < typename T, template<typename, typename> class >
class Container;
then the "set" template yields an error, and "list" works fine.
<code>
#include <set>
#include <list>
template< typename T > class AccessPolicy;
template< typename T > class AccessPolicy< std::set<T> >
{
public:
typedef std::set<T> Cont;
static void add( Cont& cont, T const& id )
{
cont.insert( id );
}
};
template< typename T > class AccessPolicy< std::list<T> >
{
public:
typedef std::list<T> Cont;
static void add( Cont& cont, T const& id )
{
cont.push_back( id );
}
};
template<
typename CType,
template< typename T > class Access = AccessPolicy
>
class Container
{
private:
CType data;
public:
typedef typename CType::value_type Id;
void add( Id const& id )
{
Access<CType>::add( data, id );
}
};
int main()
{
using namespace std;
Container< std::list<double> > a;
Container< std::set<double> > b;
a.add( 0 );
b.add( 0 );
}
</code>
Cheers & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?