473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

templates - container traits

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<int,allocator<int> > &, binder2nd<less<int> > &, int)'
traits.h:34: candidates are: void remove_if<vector<int,allocator<int>
, binder2nd<less<int> > >(vector<int,allocator<int> > &, binder2nd<less<int> >, associative_container_tag)
traits.h:48: void remove_if<vector<int,allocator<int>, binder2nd<less<int> > >(vector<int,allocator<int> > &,

binder2nd<less<int> >, sequence_tag)

Thanks for the help.
Jul 19 '05 #1
0 3731

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Anthony Heading | last post by:
Hi all, I've often found myself wanting to write code like the example here. Since both MSVC and gcc both reject it, I suspect it is indeed illegal. gcc: no type named `Name' in `class...
7
by: Jorge Schramm | last post by:
Hi, a collegue of mine is trying to write a serialisable container (reads at construction, writes at destruction). The writing part is pretty easy: simply iterate through the container and...
7
by: Jürgen Kaminski | last post by:
Hello all, I got some legacy code where a template implements a non-templated interface. These classes are really HUGE, but it boils down to the following structure: class BaseClass { public:...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
3
by: Benedikt Weber | last post by:
I found the following compiler error with Microsoft Visual C++ .NET. I use different functions with return types determined by a Traits class. Function g() below works ok, but when I put the two...
6
by: Josefo | last post by:
Hello all. I am a newbie following the C++ tutorial in : http://www.cplusplus.com/doc/tutorial/templates.html I am unable to succesfully compile any of the examples with templates of this...
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
8
by: Mosfet | last post by:
Hi, How can I transform the following functions into templated version : class foo { public: Ret_t GetProfileCount( UShort_t& a_nCount ) {
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.