Original Post
🔒 Locked
Templates
Advertisement
None. (Of which I am aware.)
My guess is that use of the "class" keyword came first. People in charge of the C++ standard really don't like to make new keywords, and generally will reuse existing keywords if they can, thus that's likely what they were thinking when they used "class" here. However, in some situations you need to manually specify that something is the name of a type, rather than a name of an object, for instance, since the compiler doesn't have enough information to figure it out on its own. So they had to introduce a new keyword anyway, and "typename" made sense. Once they had the keyword, they realized that "typename" made a lot of sense in the template doohickey, too, rather than "class", because in many cases you use types that are not classes (fundamental types, structs, and so forth), so they allowed use of the keyword "typename". Like I said, this is all speculation, but if fits what I've read about the people in charge of the C++ standard.
"typename" is the keyword I personally choose to use, largely because as I said above, you're not always specifying a class as the template type.
My guess is that use of the "class" keyword came first. People in charge of the C++ standard really don't like to make new keywords, and generally will reuse existing keywords if they can, thus that's likely what they were thinking when they used "class" here. However, in some situations you need to manually specify that something is the name of a type, rather than a name of an object, for instance, since the compiler doesn't have enough information to figure it out on its own. So they had to introduce a new keyword anyway, and "typename" made sense. Once they had the keyword, they realized that "typename" made a lot of sense in the template doohickey, too, rather than "class", because in many cases you use types that are not classes (fundamental types, structs, and so forth), so they allowed use of the keyword "typename". Like I said, this is all speculation, but if fits what I've read about the people in charge of the C++ standard.
"typename" is the keyword I personally choose to use, largely because as I said above, you're not always specifying a class as the template type.
Quote:
None. (Of which I am aware.)
Also there aren't any which you aren't aware of, "There is no semantic difference between class and typename in a template-parameter." (taken directly from the standard)
There have been some discussion about which is best to use, both have other uses. Personally I use class, just in case I need to use dependent types as a template parameter, for example if we for some reason wanted to create a function to test if a string contains a character given as a template parameter:
template<typename T,typename T::value_type Ch>bool contains(const T& str){ typename T::size_type index = str.find( Ch ); return (T::npos != index);}When you first look at it, the second template parameter could look like a type, but actually we just need typename because the name is dependent. So the following would be valid:
contains<std::string,'a'>("abc");IMO it would be cleaner to use class for template parameters, so the original code would look like this:
template<class T,typename T::value_type Ch>bool contains(const T& str){ typename T::size_type index = str.find( Ch ); return (T::npos != index);}The problem with class is that it seems to suggest that it's for classes, but int for example isn't a class and it's still valid to use that.
Continuing your train of thought, lets adjust the standard ;)
typename - any type
primitive - any of the build-in types
class - any class
This should allows the developer to control what kind of types should be allowed for the template he is working on.
typename - any type
primitive - any of the build-in types
class - any class
This should allows the developer to control what kind of types should be allowed for the template he is working on.
Quote:
Original post by Kalasjniekof
Continuing your train of thought, lets adjust the standard ;)
typename - any type
primitive - any of the build-in types
class - any class
This should allows the developer to control what kind of types should be allowed for the template he is working on.
As Agony said, the reason for keeping both class and typename isn't to give the developer a choice. The problem is that before the standarization of C++, C++ constantly changed. When templates was first introduced only the class keyword was available, some time later the typename keyword was introduced (I don't know exactly when), and when C++ was finally standarized, then the people behind the standard wanted to keep backwards-compability with both code using typename and class. (All the info said here is based on statements in the book "C++ Templates: The Complete Guide", and not actual experience, so please correct me if I'm wrong)
I don't think using different keywords for built-in types and classes should be used (and even if I thought it wouldn't matter, the C++ commitee would never implement this). Both classes and built-in types are types; and classes should be treated as any other type. In case you actually need different behavior, then that is also possible without the need for new keywords.
being that a class is a type (technically it's the definition for a type) then readjusting the standard becomes an effort in redundancy ;)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement