C ++ 11 "class" keyword

I recently started using C ++ 11 a bit more and ask a few questions about the special use of the class keyword. I know what he used to declare the class, but there are two instances that I saw there, I don’t understand:

Method<class T>();

and

class class_name *var;

Why do we precede the type name with the keyword class in the first example, and what does the keyword do with the pointer in the second example?

+4
source share
2 answers

This is known as a specified type specifier and is usually only required when your class name is "obscured" 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 the following 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;
      ^

, .

:

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.

.

class T
{
public:
    using value_type = int;
};

// method implementation...

Method<T>();
Method<class T>(); // class is redundant here
+11

, class . . :

class SomeClass;
SomeClass* p = some_function();

:

class SomeClass* p = some_function();

, , - , . :.

template<class Tag> struct Node { Node* next; };

struct Some : Node<class Tag1>, Node<class Tag2> {};
+2

All Articles