Re: GCC error? (was: How to you access a nested template class?)
On Nov 28, 12:56 pm, Adam Nielsen
<adam.niel...@remove.this.uq.edu.au> wrote:
I cannot reproduce the error. The code you posted compiles
for me. This maybe due to you giving instructions on how to
create the code from the present pieces instead of posting a
complete piece.
That's strange then - well here is the exact code I'm
compiling. Perhaps if someone else using GCC can test it as
well to see whether it's a compiler issue?
#include <cassert>
#include <map>
template < typename T >
class typemap {
typedef void (*id) ( void );
template < typename A >
static
void type_identifier ( void ) {}
std::map< id, T > data;
public:
template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifier<A> ] );
}
template < typename A >
T & value ( void ) {
return ( data[ &type_identifier<A> ] );
}
};
template <typename X>
int m ( void ) {
typemap<int> table;
table.value<char>() = 2;
table.value<int>() = 1;
assert( table.value<char>() == 2 );
assert( table.value<int>() == 1 );
}
int main ( void ) {
m<int>();
}
$ g++ -o t t.cpp
t.cpp: In function `int m()':
t.cpp:32: syntax error before `>' token
t.cpp:33: syntax error before `>' token
t.cpp:34: syntax error before `>' token
t.cpp:35: syntax error before `>' token
$ g++ --version
g++-gcc-3.2.3 (GCC) 3.2.3
I've also tried it with GCC 3.3.4 with the same result.
I get the same errors with 3.2.3, but it compiles with g++
4.1.0. If you replace typemap<int> with typemap<X> in the first
line of m, you get errors with both compilers, of course.
Changing each of the function calls to:
table.template value<...>...
works in all cases however.
It's an interesting case, since it's the only case I know of off
hand in which a (non-instantiated) template definition can
trigger the instantiation of another template. Apparently, g++
pre-4.0 is not instantiating typemap<int> here, and so is
treating the < as if it were the less than operator. (Note that
in the case of typemap<X>, the context is dependent, so you are
required to tell the compiler when the name in question -- here,
value -- is a type or a template.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34