Template as a base class.

K

Kyku

Hello, I'm having a problem with the following code. G++ complains
that:

assoc.cpp: In member function 'V& Assoc<K, V>::ValueOf(const K&)':
assoc.cpp:35: error: there are no arguments to 'begin' that depend on a
template parameter, so a declaration of 'begin' must be available
assoc.cpp:35: error: (if you use '-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)

Prefixing begin() with this->begin() or std::list said:
::begin() solves the problem. But I wonder why it can't look for the function in the base class.

Please explain.
Thanks in advance, Kyku


#include <list>
#include <utility>
#include <algorithm>

struct KeyNotFound {};

// This is to compare a key given in the constructor to the key in a
pair.
template < typename K , typename V >
struct KeyEqual
{
public:
KeyEqual (const K& key) : _M_value (key) {}

bool operator () (const std::pair< K, V >& pair) const
{
return _M_value == pair.first;
}

private:
K _M_value;
};

template < typename K, typename V >
class Assoc : public std::list< std::pair< K, V > >
{
typedef typename std::list< std::pair< K, V > >::iterator iterator;
public:
void Insert (const K& key, const V& val) throw (std::bad_alloc)
{
push_back (make_pair (key, val));
}

V& ValueOf (const K& key) throw (KeyNotFound)
{
iterator iter;
iter = std::find_if (begin (), this->end (), KeyEqual< K, V >
(key)); // <<- HERE IS THE PROBLEM
if (iter == this->end ()) throw KeyNotFound ();
return iter->second;
}

bool Exists (const K& key) throw ()
{
return std::find (this->begin (), this->end (), key) !=
this->end ();
}
};
 
I

Ian Collins

Kyku said:
Hello, I'm having a problem with the following code. G++ complains
that:

assoc.cpp: In member function 'V& Assoc<K, V>::ValueOf(const K&)':
assoc.cpp:35: error: there are no arguments to 'begin' that depend on a
template parameter, so a declaration of 'begin' must be available
assoc.cpp:35: error: (if you use '-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)

Prefixing begin() with this->begin() or std::list<std::pair<K, V>

::begin() solves the problem. But I wonder why it can't look for the function in the base class.

Section 14.6.2 §3 of the 2003 revision of the standard states: "... the
base class scope is not examined during unqualified name lookup either
at the point of definition of the class template or member or during an
instantiation of the class template or member."

This is a clarification of the original wording that was ambiguous.
 
B

Bo Persson

Kyku said:
Hello, I'm having a problem with the following code. G++ complains
that:

assoc.cpp: In member function 'V& Assoc<K, V>::ValueOf(const K&)':
assoc.cpp:35: error: there are no arguments to 'begin' that depend
on a
template parameter, so a declaration of 'begin' must be available
assoc.cpp:35: error: (if you use '-fpermissive', G++ will accept
your
code, but allowing the use of an undeclared name is deprecated)



Please explain.
Thanks in advance, Kyku

There could be specializations of the base classes that not all
contain a begin() function. To check this, the compiler could have to
instantiate the base class for all combinations of K and V.

Also, if there is also a namespace scope begin() function available,
should it choose that one when it is not present in the base class?
Not a good idea!


Bo Persson
 
J

Jens Theisen

Bo said:
There could be specializations of the base classes that not all
contain a begin() function. To check this, the compiler could have to
instantiate the base class for all combinations of K and V.

And, during parsing, it needs to know what's a type or a template,
because C++'grammer is screwed.

If C++'s grammer was slightly more sane, none of this would be necessary.

Jens
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,588
Members
45,094
Latest member
PollyBlau4

Latest Threads

Top