Re: Keywords 'class' and 'typename' not interchangeable with template template parameters?
Am 19.04.2011 22:41, schrieb Matthias Hofmann:
Hello everybody!
I just stumbled into the miraculous world of template template parameters
and wrote a short piece of testing code to play around with this rather
new
C++ feature:
[...]
I was glad to learn that despite warnings about compatibility issues,
Visual
C++ 2005 Express Edition seems to offer full support for template template
parameters. The only thing that struck me, and this brings us to my
question, is the use of the 'class' keyword in the declaration of
'MyClass':
template<template<typename, typename> class Cont, typename T>
class MyClass { ... };
I adopted this syntax from the code examples I found on the internet, but
I
thought that the keywords 'class' and 'typenames' were perfectly
interchangeable with regards to templates, and that preference of one
towards the other was just a question of style? However, my code does not
compile any more when I replace the keyword 'class' with 'typename':
template<template<typename, typename> typename Cont, typename T>
class MyClass { ... };
Is this an issue with my compiler, or does the standard in fact require
the
'class' keyword when it comes to template template parameters? This would
surprise me, as in my example, the 'typename' keyword is used to define
the
parameters of 'Cont'. So what about interchangeability of the 'class' and
'typename' keywords now?
The language does not give you the guarantee that bothe keywords are
interchangeable. If this would be the case, your could define a class
type by writing instead:
typename A {};
which is no valid C++. There is only *one* place, where both keywords
can be used with the same meaning and that is as part of a template type
parameter declaration and that is not a template template parameter.
This also means that you cannot use the keyword 'class' as a prefix in
the way you would do that with 'typename' as in:
template<class T>
struct ident { typedef T type; };
template<class T>
typename ident<T>::type foo();
which cannot be transformed into
template<class T>
struct ident { typedef T type; };
template<class T>
class ident<T>::type foo();
for example.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]