C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Strange gcc behaviour or ... ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
gino
Guest





PostPosted: Tue Mar 15, 2005 4:01 pm    Post subject: Strange gcc behaviour or ... ? Reply with quote



Hi everybody,

while playing around with a map-based class, I stumbled into a strange
gcc behaviour. The code goes like this:

template<typename K, class D>
class Directory
{
typedef std::map<K, D> MapType;
typedef MapType::value_type Pair; // won't compile

public:
typedef MapType::iterator MapIter; // won't compile

Directory() {}
~Directory() {}

void add(D d, K k) { map_.insert(Pair(k, d)); }
D get(K k) { return map_[k]; }
MapIter begin() { return map_.begin(); }
MapIter end() { return map_.end(); }

private:
MapType map_;

};

When trying to compile the above with gcc 3.4.3 (20050110), I get these
errors:

directory.h:17: error: type `std::map<K, D, std::less<_Key>,
std::allocator<std::pair >' is not derived from type
`Directory<K, D>'
[...]
directory.h:20: error: type `std::map<K, D, std::less<_Key>,
std::allocator<std::pair >' is not derived from type
`Directory<K, D>'
[...]

If I substitute the template with explicit types (i.e. I manually
change D and K with real types), the code compiles (and works) cleanly.
Even stranger, the above code compiles cleanly with Visual C++.

Any ideas or suggestions?

Thanks and regards,
Gianluca


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Tue Mar 15, 2005 8:26 pm    Post subject: Re: Strange gcc behaviour or ... ? Reply with quote



gino wrote:
Quote:
template<typename K, class D
class Directory
{
typedef std::map typedef MapType::value_type Pair; // won't compile

typedef typename MapType::value_type Pair;

Quote:
public:
typedef MapType::iterator MapIter; // won't compile

"typename" keyword is missing, too. It is required whenever you refer
to a type that depends on the template parameters. Some older (pre-
standard) compilers let you go away without it...

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marco Manfredini
Guest





PostPosted: Tue Mar 15, 2005 8:41 pm    Post subject: Re: Strange gcc behaviour or ... ? Reply with quote



gino wrote:

Quote:
typedef MapType::value_type Pair; // won't compile
must be

typedef typename MapType::value_type Pair;

Quote:
typedef MapType::iterator MapIter; // won't compile
must be

typedef typename MapType::iterator MapIter;

Quote:
When trying to compile the above with gcc 3.4.3 (20050110), I get
these errors:
Well, the diagnostics are a little wacky. gcc-3.3.4 would tell you about

the missing 'typename'

Quote:
If I substitute the template with explicit types (i.e. I manually
change D and K with real types), the code compiles (and works)
cleanly. Even stranger, the above code compiles cleanly with Visual
C++.

It shouldn't compile, but you can't have everything. gcc is allowed to
spill out unintelligible error messages, because MapType *depends on
template parameters* -- in that case 'typename' is required to qualify
MapType::value_type and MapType::iterator as types.

--
Marco


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Maeder
Guest





PostPosted: Tue Mar 15, 2005 8:45 pm    Post subject: Re: Strange gcc behaviour or ... ? Reply with quote

"gino" <insol (AT) tiscalinet (DOT) it> writes:

Quote:
template<typename K, class D
class Directory
{
typedef std::map typedef MapType::value_type Pair; // won't compile

The name value_type depends on the template parameter and names a
type. Such names have to be prefixed with the keyword typename:

typedef typename MapType::value_type Pair;


Quote:
public:
typedef MapType::iterator MapIter; // won't compile

typedef typename MapType::iterator MapIter;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Mar 16, 2005 8:56 am    Post subject: Re: Strange gcc behaviour or ... ? Reply with quote

gino wrote:
Quote:
Hi everybody,

while playing around with a map-based class, I stumbled into a strange
gcc behaviour. The code goes like this:

It is not strange but correct.

Quote:
template<typename K, class D
class Directory
{
typedef std::map typedef MapType::value_type Pair; // won't compile

public:
typedef MapType::iterator MapIter; // won't compile
snip


Right. The compiler doesn't know that the value_type and iterator
members of every instance of std::map are types, since templates may
be specialised. Without knowing that, it cannot parse these lines.
You need to help it by putting "typename" after "typedef" on these
lines. There's more information on the page referred to in my
signature.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu Mar 17, 2005 8:12 am    Post subject: Re: Strange gcc behaviour or ... ? Reply with quote

Falk Tannhäuser wrote:
Quote:
gino wrote:
template<typename K, class D
class Directory
{
typedef std::map typedef MapType::value_type Pair; // won't compile

typedef typename MapType::value_type Pair;

public:
typedef MapType::iterator MapIter; // won't compile

"typename" keyword is missing, too. It is required whenever
you refer to a type that depends on the template
parameters. Some older (pre- standard) compilers let you go
away without it...

Actually, given correct flags, any compiler of reasonable
quality will accept it, given the length of time it was
acceptable.

The ideal situation, of course, is that the compiler defaults to
requiring standard conforming code, but has options to support
existing code. (I think that the EDG front-end can be
configured this way.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.