Skip to main content

IAR Embedded Workbench for RX 5.20

C++ language extensions

In this section:

When you use the compiler in C++ mode and enable IAR language extensions, the following C++ language extensions are available in the compiler:

  • In a friend declaration of a class, the class keyword can be omitted, for example:

    class B;
    class A
    {
      friend B;       //Possible when using IAR language
                      //extensions
      friend class B; //According to the standard
    };
  • In the declaration of a class member, a qualified name can be used, for example:

    struct A 
    {
      int A::F(); // Possible when using IAR language extensions
      int G();    // According to the standard
    };
  • It is permitted to use an implicit type conversion between a pointer to a function with C linkage (extern "C") and a pointer to a function with C++ linkage (extern "C++"), for example:

    extern "C" void F(); // Function with C linkage
    void (*PF)()       // PF points to a function with C++ linkage
                 = &F; // Implicit conversion of function pointer. 

    According to the standard, the pointer must be explicitly converted.

  • If the second or third operands in a construction that contains the ?operator are string literals or wide string literals—which in C++ are constants—the operands can be implicitly converted to char * or wchar_t *, for example:

    bool X;
    
    char *P1 = X ? "abc" : "def";       //Possible when using IAR
                                        //language extensions
    char const *P2 = X ? "abc" : "def";//According to the standard
  • Default arguments can be specified for function parameters not only in the top-level function declaration, which is according to the standard, but also in typedef declarations, in pointer-to-function function declarations, and in pointer-to-member function declarations.

  • In a function that contains a non-static local variable and a class that contains a non-evaluated expression—for example a sizeof expression—the expression can reference the non-static local variable. However, a warning is issued.

  • An anonymous union can be introduced into a containing class by a typedef name. It is not necessary to first declare the union. For example:

    typedef union 
    {
      int i,j;
    } U;  // U identifies a reusable anonymous union.
    
    class A
    {
    public:
      U;  // OK ‑‑ references to A::i and A::j are allowed.
    };

    In addition, this extension also permits anonymous classes and anonymous structs, as long as they have no C++ features—for example, no static data members or member functions, and no non-public members—and have no nested types other than other anonymous classes, structs, or unions. For example:

    struct A
    {
      struct
      {
        int i,j;
      }; // OK ‑‑ references to A::i and A::j are allowed.
    };
  • The friend class syntax allows non-class types as well as class types expressed through a typedef without an elaborated type name. For example:

    typedef struct S ST;
    
    class C
    {
    public:
      friend S;  // Okay (requires S to be in scope)
      friend ST; // Okay (same as "friend S;")
      // friend S const; // Error, cv-qualifiers cannot
                         // appear directly
    };
  • It is allowed to specify an array with no size or size 0 as the last member of a struct. For example:

    typedef struct
    {
      int i;
      char ir[0]; // Zero-length array
    };
    
    typedef struct
    {
      int i;
      char ir[];  // Zero-length array
    };
  • Arrays of incomplete types

    An array can have an incomplete struct, union, enum, or class type as its element type. The types must be completed before the array is used—if it is— or by the end of the compilation unit—if it is not.

  • Concatenating strings

    Mixed string literal concatenations are accepted.

    wchar_t * str = "a" L "b";
  • Trailing comma

    A trailing comma in the definition of an enumeration type is silently accepted.

Except where noted, all of the extensions described for C are also allowed in C++ mode.

Note

If you use any of these constructions without first enabling language extensions, errors are issued.