Reputation: 1533
I've recently started using C++ 11 a little bit more and have a few questions on special uses of the class keyword. I know it's used to declare a class, but there's two instances I have seen that I don't understand:
Method<class T>();
and
class class_name *var;
Why do we precede the typename by the keyword class in the first example, and what does the keyword do the pointer in the second example?
Upvotes: 4
Views: 2252
Reputation: 322
That is known as an elaborated type specifier and is generally only necessary when your class name is "shadowed" or "hidden" and you need to be explicit.
class T
{
};
// for the love of god don't do this
T T;
T T2;
If your compiler is smart, it will give you these warnings:
main.cpp:15:5: error: must use 'class' tag to refer to type 'T' in this scope
T T2;
^
class
main.cpp:14:7: note: class 'T' is hidden by a non-type declaration of 'T' here
T T;
^
If your class is not previously defined, it will also act as a forward declaration.
For example:
template <typename T>
void Method()
{
using type = typename T::value_type;
}
Method<class T>(); // T is a forward declaration, but we have not defined it yet,
// error.
Otherwise it's not necessary.
class T
{
public:
using value_type = int;
};
// method implementation...
Method<T>();
Method<class T>(); // class is redundant here
Upvotes: 11
Reputation: 136246
In addition to other answers, you can you class
keyword to forward declare a class in the usage spot. E.g. instead of:
class SomeClass;
SomeClass* p = some_function();
You can write:
class SomeClass* p = some_function();
This is often used with templates and tag dispatching, when instantiating a template requires a tag type argument whose only purpose is to make the instantiation a unique type and the tag does not have to be a complete type. E.g.:
template<class Tag> struct Node { Node* next; };
struct Some : Node<class Tag1>, Node<class Tag2> {};
Upvotes: 2