Introduces an **elaborated type specifier,** which specifies that the following name is the name of a class type. If the class name has been declared already, it can be found even if hidden by another name. If the class name has not been declared already, it is forward-declared.
classfoo;// elaborated type specifier -> forward declarationclassbar{public:bar(foo& f);};voidbaz();classbaz;// another elaborated type specifer; another forward declaration// note: the class has the same name as the function void baz()classfoo{
bar b;friendclassbaz;// elaborated type specifier refers to the class,// not the function of the same namepublic:foo();};
Introduces a type parameter in the declaration of a [template](http://stackoverflow.com/documentation/c%2b%2b/460/templates).
template<classT>const T&min(const T& x,const T& y){return b < a ? b : a;}
In the declaration of a [template template parameter](http://stackoverflow.com/documentation/c%2b%2b/460/templates/10838/template-template-parameters), the keyword `class` precedes the name of the parameter. Since the argument for a template template parameter can only be a class template, the use of `class` here is redundant. However, the grammar of C++ requires it.
template<template<classT>classU>// ^^^^^ "class" used in this sense here;// U is a template template parametervoidf(){U<int>::do_it();U<double>::do_it();}
Note that sense 2 and sense 3 may be combined in the same declaration. For example:
template<classT>classfoo{};
foo<classbar> x;// <- bar does not have to have previously appeared.
In the declaration or definition of an enum, declares the enum to be a [scoped enum](http://stackoverflow.com/documentation/c%2b%2b/2796/enumeration/13318/scoped-enums#t=201608050832023792595).
enumclassFormat{
TEXT,
PDF,
OTHER,};
Format f = F::TEXT;
enum
Introduces the definition of an [enumeration type](http://stackoverflow.com/documentation/c%2b%2b/2796/enumeration).
enumDirection{
UP,
LEFT,
DOWN,
RIGHT
};
Direction d = UP;
In C++11, enum may optionally be followed by class or struct to define a scoped enum(opens new window). Furthermore, both scoped and unscoped enums can have their underlying type explicitly specified by : T following the enum name, where T refers to an integer type.
enum class Format : char {
TEXT,
PDF,
OTHER
};
Format f = Format::TEXT;
enum Language : int {
ENGLISH,
FRENCH,
OTHER
};
Enumerators in normal enums may also be preceded by the scope operator, although they are still considered to be in the scope the enum was defined in.
Language l1, l2;
l1 = ENGLISH;
l2 = Language::OTHER;
Introduces an **elaborated type specifier,** which specifies that the following name is the name of a previously declared enum type. (An elaborated type specifier cannot be used to forward-declare an enum type.) An enum can be named in this way even if hidden by another name.
enumFoo{ FOO };voidFoo(){}
Foo foo = FOO;// ill-formed; Foo refers to the functionenumFoo foo = FOO;// ok; Foo refers to the enum type
Introduces an **opaque enum declaration,** which declares an enum without defining it. It can either redeclare a previously declared enum, or forward-declare an enum that has not been previously declared.
An enum first declared as scoped cannot later be declared as unscoped, or **vice versa.** All declarations of an enum must agree in underlying type.
When forward-declaring an unscoped enum, the underlying type must be explicitly specified, since it cannot be inferred until the values of the enumerators are known.
enumclassFormat;// underlying type is implicitly intvoidf(Format f);enumclassFormat{
TEXT,
PDF,
OTHER,};enumDirection;// ill-formed; must specify underlying type
If a class type is defined using the keyword struct, then the default accessibility of bases and members is public rather than private.
struct cannot be used to declare a template type parameter or template template parameter; only class can.
union
Introduces the definition of a [union](http://stackoverflow.com/documentation/c%2b%2b/2678/unions) type.
// Example is from POSIXunion sigval {int sival_int;void*sival_ptr;};
Introduces an **elaborated type specifier,** which specifies that the following name is the name of a union type. If the union name has been declared already, it can be found even if hidden by another name. If the union name has not been declared already, it is forward-declared.
union foo;// elaborated type specifier -> forward declarationclassbar{public:bar(foo& f);};voidbaz();union baz;// another elaborated type specifer; another forward declaration// note: the class has the same name as the function void baz()union foo {long l;union baz* b;// elaborated type specifier refers to the class,// not the function of the same name};