Why C++ Supports both Class and Typename for Type Parameters
Recently, someone asked me why we support both class and typename within C++ to indicate a type parameter since the keywords do not hold any platform significance – for example, class is not meant to suggest a native type nor is typename meant to suggest a CLI type. Rather, both equivalently indicate that the name following represents a parameterized type placeholder that will be replaced by a user-specfied actual type.
The reason for the two keywords is historical. In the original template specification, Stroustrup reused the existing class keyword to specify a type parameter rather than introduce a new keyword that might of course break existing programs. It wasn't that a new keyword wasn't considered -- just that it wasn't considered necessary given its potential disruption. And up until the ISO-C++ standard, this was the only way to declare a type parameter.
Reuses of existing keywords seems to always sow confusion. What we found is that beginners were whether the use of the class constrained or limited the type arguments a user could specify to be class types rather than, say, a built-in or pointer type. So, there was some feeling that not having introduced a new keyword was a mistake.
During standardization, certain constructs were discovered within a template definition that resolved to expressions although they were meant to indicate declarations. For example,
template <class T>
class Demonstration {
public:
void method() {
T::A *aObj; // oops …
// …
};
While the statement containing aObj is intended by the programmer to be interpreted as the declaration of a pointer to a nested type A within the type parameter T, the language grammar interprets it as an arithmetic expression multiplying the static member A of type T with aObj and throwing away the result. Isn't that annoying! (This sort of dilemna is not possible within generics – there is no way to safely verify that any T contains an A so that the runtime can safely construct an instance of the generic type.)
The committee decided that a new keyword was just the ticket to get the compiler off its unfortunate obsession with expressions. The new keyword was the self-describing typename. When applied to a statement, such as,
typename T::A* a6; // declare pointer to T ’s A
it instructs the compiler to treat the subsequent statement as a declaration. Since the keyword was on the payroll, heck, why not fix the confusion caused by the original decision to reuse the class keyword. Of course, given the extensive body of existing code and books and articles and talks and postings using the class keyword, they chose to also retain support for that use of the keyword as well. So that's why you have both.
Comments
- Anonymous
August 11, 2004
Cool - thanks for the info :-) - Anonymous
August 11, 2004
So is there ever a good reason to use "class" rather than "typename" for a type parameter, or is it purely for backwards compatibility? - Anonymous
August 11, 2004
is there ever a `good' reason? no, if you feel the need to converge on a single naming convention, typename is clearer, i believe, to those fresh to the language. however, if you analyze the necessary keystrokes, typename spans the entire keyboard and so is not just longer to type, but requires greater facility -- of course, that is said tongue-in-cheek. - Anonymous
August 12, 2004
:-) Good answer. Thanks. - Anonymous
August 13, 2004
I just had to use typename for the "unfortunate obsession with expressions" sense and I still don't understand why. Consider the following moronic way to find the length of a vector:
using namespace std;
template <class T> int vcount(vector<T> v) {
int i;
typename vector<T>::iterator vi;
for(i=0,vi=v.begin(); vi!=v.end(); i++,vi++)
;
return(i);
}
Much to my surprise, g++ 3.4.0 didn't like vector<T>::iterator until I put a typename before it. (Looks like older g++'s think it's just fine.)
I remain baffled, but at least typename was there for me! - Anonymous
October 31, 2007
PingBack from http://openandfree.wordpress.com/2007/10/31/t%e1%bb%ab-khoa-typename-trong-c/