Getting started with C++TemplatesMetaprogrammingIteratorsReturning several values from a functionstd::stringNamespacesFile I/OClasses/StructuresSmart PointersFunction Overloadingstd::vectorOperator OverloadingLambdasLoopsstd::mapThreadingValue CategoriesPreprocessorSFINAE (Substitution Failure Is Not An Error)The Rule of Three, Five, And ZeroRAII: Resource Acquisition Is InitializationExceptionsImplementation-defined behaviorSpecial Member FunctionsRandom number generationReferencesSortingRegular expressionsPolymorphismPerfect ForwardingVirtual Member FunctionsUndefined BehaviorValue and Reference SemanticsOverload resolutionMove SemanticsPointers to membersPimpl Idiomstd::function: To wrap any element that is callableconst keywordautostd::optionalCopy ElisionBit OperatorsFold ExpressionsUnionsUnnamed typesmutable keywordBit fieldsstd::arraySingleton Design PatternThe ISO C++ StandardUser-Defined LiteralsEnumerationType ErasureMemory managementBit ManipulationArraysPointersExplicit type conversionsRTTI: Run-Time Type InformationStandard Library AlgorithmsFriend keywordExpression templatesScopesAtomic Typesstatic_assertoperator precedenceconstexprDate and time using <chrono> headerTrailing return typeFunction Template OverloadingCommon compile/linker errors (GCC)Design pattern implementation in C++Optimization in C++Compiling and BuildingType Traitsstd::pairKeywordsOne Definition Rule (ODR)Unspecified behaviorFloating Point ArithmeticArgument Dependent Name Lookupstd::variantAttributesInternationalization in C++ProfilingReturn Type CovarianceNon-Static Member FunctionsRecursion in C++Callable Objectsstd::iomanipConstant class member functionsSide by Side Comparisons of classic C++ examples solved via C++ vs C++11 vs C++14 vs C++17The This PointerInline functionsCopying vs AssignmentClient server examplesHeader FilesConst Correctnessstd::atomicsData Structures in C++Refactoring TechniquesC++ StreamsParameter packsLiteralsFlow ControlType KeywordsBasic Type KeywordsVariable Declaration KeywordsIterationtype deductionstd::anyC++11 Memory ModelBuild SystemsConcurrency With OpenMPType Inferencestd::integer_sequenceResource Managementstd::set and std::multisetStorage class specifiersAlignmentInline variablesLinkage specificationsCuriously Recurring Template Pattern (CRTP)Using declarationTypedef and type aliasesLayout of object typesC incompatibilitiesstd::forward_listOptimizationSemaphoreThread synchronization structuresC++ Debugging and Debug-prevention Tools & TechniquesFutures and PromisesMore undefined behaviors in C++MutexesUnit Testing in C++Recursive MutexdecltypeUsing std::unordered_mapDigit separatorsC++ function "call by value" vs. "call by reference"Basic input/output in c++Stream manipulatorsC++ ContainersArithmitic Metaprogramming

Type Keywords

Other topics

class

  1. Introduces the definition of a class type.

    class foo {
        int x;
      public:
        int get_x();
        void set_x(int new_x);
    };
    
  2. 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.

    class foo; // elaborated type specifier -> forward declaration
    class bar {
      public:
        bar(foo& f);
    };
    void baz();
    class baz; // another elaborated type specifer; another forward declaration
               // note: the class has the same name as the function void baz()
    class foo {
        bar b;
        friend class baz; // elaborated type specifier refers to the class,
                          // not the function of the same name
      public:
        foo();
    };
    
  3. Introduces a type parameter in the declaration of a template.

    template <class T>
    const T& min(const T& x, const T& y) {
        return b < a ? b : a;
    }
    
  4. In the declaration of a template template parameter, 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 <class T> class U>
    //                           ^^^^^ "class" used in this sense here;
    //                                 U is a template template parameter
    void f() {
        U<int>::do_it();
        U<double>::do_it();
    }
    
  5. Note that sense 2 and sense 3 may be combined in the same declaration. For example:

    template <class T>
    class foo {
    };
    
    foo<class bar> x; // <- bar does not have to have previously appeared.
    
C++11
  1. In the declaration or definition of an enum, declares the enum to be a scoped enum.

    enum class Format {
        TEXT,
        PDF,
        OTHER,
    };
    Format f = F::TEXT;
    

struct

Interchangeable with class, except for the following differences:

  • 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.

enum

  1. Introduces the definition of an enumeration type.

    enum Direction {
        UP,
        LEFT,
        DOWN,
        RIGHT
    };
    Direction d = UP;
    
C++11

In C++11, enum may optionally be followed by class or struct to define a scoped enum. 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;
  1. 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.

    enum Foo { FOO };
    void Foo() {}
    Foo foo = FOO;      // ill-formed; Foo refers to the function
    enum Foo foo = FOO; // ok; Foo refers to the enum type
    
C++11
  1. 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.

    enum class Format; // underlying type is implicitly int
    void f(Format f);
    enum class Format {
        TEXT,
        PDF,
        OTHER,
    };
    
    enum Direction;    // ill-formed; must specify underlying type
    

union

  1. Introduces the definition of a union type.

    // Example is from POSIX
    union sigval {
        int     sival_int;
        void   *sival_ptr;
    };
    
  2. 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 declaration
    class bar {
      public:
        bar(foo& f);
    };
    void baz();
    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
    };
    

Contributors

Topic Id: 7838

Example Ids: 18504,18505,18566,19092

This site is not affiliated with any of the contributors.