templates - container traits

S

skscpp

What's wrong with the following code? Compiler error is at the bottom.
The compiler I am using is gcc version 2.95.

=============================
// traits.h
=============================
#include <vector>
#include <list>
#include <set>
#include <functional>
#include <iostream>

struct container_tag { container_tag() { cout << "container_tag\n"; }
};
struct sequence_tag { sequence_tag() { cout << "sequence_tag\n"; }};
struct associative_container_tag { associative_container_tag() { cout
<< "associative_container_tag\n"; }};

template <class Container>
struct container_traits {
typedef container_tag container_category;
};

template <class T, class Allocator>
struct container_traits<std::vector<T, Allocator> > {
typedef sequence_tag container_category;
};

template <class T, class Allocator>
struct container_traits<std::list<T, Allocator> > {
typedef sequence_tag container_category;
};

template <class T, class Allocator>
struct container_traits<std::set<T, Allocator> > {
typedef associative_container_tag container_category;
};

template <class AssociativeContainer, class Predicate>
inline void remove_if(AssociativeContainer& C, Predicate pred,
class associative_container_tag)
{
typedef typename AssociativeContainer::iterator iterator;
iterator cur = c.begin();
const iterator last = c.end();

while ( (cur = std::find_if(cur, last, pred)) != last)
{
iterator tmp = cur++;
c.erase(tmp);
}
}

template<class Sequence, class Predicate>
inline void remove_if(Sequence& s, Predicate p, class sequence_tag)
{
s.erase( std::remove_if(s.begin(), s.end(), p), s.end() );
}

template < class Container, class Predicate>
inline void remove_if(Container& c, Predicate p)
{
typedef typename container_traits< c >::container_category cat;

remove_if( c, p, cat() );
}
===============================
// test.cc
=============================
#include "traits.h"
#include <functional>

int main()
{
int A[5] = { 1, 2, -3, -4, 5 };
std::vector<int> v(A, A + 5);

remove_if( v, std::bind2nd(std::less<int>(), 0));

}
=============================


In file included from test.cc:1:
traits.h: In function `void remove_if(Container &, Predicate)':
traits.h:55: type/value mismatch at argument 1 in template parameter
list for `template <class Container> container_traits<Container>'
traits.h:55: expected a type, got `c'
traits.h:55: `container_category' is not a class or namespace
traits.h:55: ANSI C++ forbids declaration `cat' with no type
traits.h: In function `void remove_if<vector<int,allocator<int> >,
binder2nd<less<int> > >(vector<int,allocator<int> > &,
binder2nd<less<int> >)':
test.cc:10: instantiated from here
traits.h:57: no matching function for call to `remove_if
(vector said:
, binder2nd<less<int> > >(vector<int,allocator<int> > &,
associative_container_tag) said:
, binder2nd<less<int> > >(vector<int,allocator<int> > &,
binder2nd<less<int> >, sequence_tag)



Thanks for the help.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top