typedef std::pair<word, word> msg;
static inline word waiter_tag(const waiter & w) {
return w.second;
}
static std::list<msg> mpool;
typedef std::list<msg>::iterator mpool_iter;
typedef std::pair<process::id, word> waiter;
static inline process::id waiter_id(const wlist_iter & wi) {
return wi->first;
}
static std::list<waiter> waiters;
typedef std::list<Kwaiter>::iterator wlist_iter;
static bool msg_match(const msg m, word t) {
return msg_tag(m) == t;
}
static bool find_msg(word t, m_iter & mi) {
const m_iter & e = mpool.end();
const m_iter & b = mpool.begin();
mi = std::find_if(b, e, bind2nd(ptr_fun(msg_match), t));
return mi != e;
}
static bool waiter_match(const waiter w, process::id t) {
return waiter_id(w) == t;
}
static bool find_waiter(process::id t, w_iter & wi) {
const w_iter & e = waiters.end();
const w_iter & b = waiters.begin();
wi = std::find_if(b, e, bind2nd(ptr_fun(waiter_match), t));
return wi != e;
}
list<msg>, list<waiters>.
msg_match(), waiter_match().
container_type.
tag_type.
container_type::iterator.
bool.
container_type::value_type.
tag_type.
container_type::value type.
tag_type.
bool
container_type::iterator.
$ cat t.cc #includetemplate < typename container_type> bool find_value(const container_type & values) { return true; } int main() { std::vector ivec; find_value(ivec); } $ g++ -c -ansi -pedantic -Wall t.cc $
$ cat t.cc
#include <vector>
#include <algorithm>
template<typename container_type, typename tag_type>
bool find_value(
const container_type & values,
const tag_type & value)
{
const container_type::const_iterator e = values.end();
return std::find(values.begin(), e, value) != e;
}
int main() {
std::vector<int> ivec;
find_value(ivec, 3);
}
$ g++ -c -ansi -pedantic -Wall t.cc
t.cc: In function bool find_value(const container_type&,
const tag_type&):
t.cc:9: parse error before e
t.cc: In function bool find_value(const container_type&,
const tag_type&) [with container_type = std::vector<int,
std::allocator<int> >, tag_type = int]:
t.cc:15: instantiated from here
t.cc:10: e undeclared (first use this function)
t.cc:10: (Each undeclared identifier is reported only once
for each function it appears in.)
$
container_type::iterator is a type.
typedef problem in lexing-parsing.
typename keyword indicates a type.
typename in template lists.
typenametypename solves the problem.
$ cat t.cc
#include <vector>
#include <algorithm>
template<typename container_type, typename tag_type>
bool find_value(
const container_type & values,
const tag_type & value)
{
const typename container_type::const_iterator e = values.end();
return std::find(values.begin(), e, value) != e;
}
int main() {
std::vector<int> ivec;
find_value(ivec, 3);
}
$ g++ -c -ansi -pedantic -Wall t.cc
$
$ cat t.cc
#include <vector>
#include <algorithm>
template < typename container_type, typename tag_type >
bool find_value(
const container_type & values,
container_type::const_iterator & vi,
const tag_type & value)
{
const typename container_type::const_iterator e = values.end();
vi = std::find(values.begin(), e, value);
return vi != e;
}
int main() {
std::vector<int> ivec;
std::vector<int>::const_iterator vi;
find_value(ivec, vi, 3);
}
$ g++ -c -ansi -pedantic -Wall t.cc
t.cc:7: type specifier omitted for parameter
t.cc:7: perhaps you want typename container_type::const_iterator
to make it a type
t.cc:7: parse error before & token
t.cc: In function int main():
t.cc:18: no matching function for call to find_value(std::vector<int,
std::allocator<int> >&, std::__normal_iterator<const int*,
std::vector<int, std::allocator<int> > >&, int)
$
typename.
$ cat t.cc #include#include template bool find_value( const container_type & values, typename container_type::const_iterator & vi, const tag_type & value) { const typename container_type::const_iterator e = values.end(); vi = std::find(values.begin(), e, value); return vi != e; } int main() { std::vector ivec; std::vector ::const_iterator vi; find_value(ivec, vi, 3); } cl g++ -c -ansi -pedantic -Wall t.cc $
template<typename container_type, typename tag_type>
bool find_value(
container_type & values,
bool (* match)(typename container_type::value_type, tag_type),
tag_type tag,
typename container_type::iterator & vi)
{
typename container_type::iterator e = values.end();
typename container_type::iterator b = values.begin();
vi = std::find_if(b, e, bind2nd(ptr_fun(match), tag));
return vi != e;
}
static bool msg_match(const msg m, word t) {
return msg_tag(m) == t;
}
static bool find_msg(word t, mpool_iter & mi) {
return find_value(mpool, msg_match, t, mi);
}
static bool waiter_match(const waiter w, process::id t) {
return waiter_id(w) == t;
}
static bool find_waiter(process::id t, wlist_iter & wi) {
return find_value(waiters, waiter_match, t, wi);
}
typename helps the compiler recognize type names.
This page last modified on 19 December 2001.