C++ 11 with Model RealTime

June 10, 2020

The 2011 version of the C++ standard introduced major enhancements to the language. Many of these enhancements have always been supported by Model RealTime since they only affect the code that you type in code snippets in the model. Examples include

However, some of the C++ language features have required explicit support in the Model RealTime user interface and model compiler to be possible to fully use in an RT model. Over several releases we have therefore gradually implemented support for those C++ 11 features in Model RealTime. In this newsletter we will briefly summarize these features.


Scoped Enumerations

A problem with C and older versions of C++ was that an enum did not define a scope. This often forced developers to use long and complex names for enum literals to avoid name clashes between literals of different enums. Another problem was that the developer could not control which underlying type the compiler should use for implementing the enum. C++ 11 introduces scoped enums which solve both these problems.

In Model RealTime you can create a scoped enum by setting a property Scoped on an Enumeration. You can also specify the underlying type using a property Type.

Note that the Type property can be set even for unscoped enumerations.

In-class member initializers

C++ 11 has a uniform initialization syntax and the possibility to initialize class members in the class definition itself. Model RealTime supports this by means of two new kinds of initializers for attributes:

int a { 4 };
int a = 4;

Use the property Initializer Kind on the attribute to specify the kind of initialization to be used.

Rvalue references

Reference types in older versions of C++ can only bind to so called lvalues (an expression representing an identifiable location in memory). C++ 11 introduces a new reference type called rvalues, which can bind to non-lvalues, such as temporary objects or literals.

Model RealTime now supports the syntax for rvalue references (&&) when defining attributes or parameters in the model. For example, this makes it possible to define move constructors or move assignment operators. For certain classes this can be important in order to implement move semantics as efficiently as possible.

Defaulted and deleted functions

C++ 11 allows functions to be marked as default or delete. Creating a defaulted constructor can be useful for classes that have user-defined constructors (since the compiler otherwise won't generate a default constructor for such classes). The delete specifier can for example be used to delete the copy constructor or assignment operator that otherwise would be automatically generated by the compiler.

Since both defaulted and deleted functions mostly are useful on the special member functions that a class may have, such as constructors or assignment operators, Model RealTime provides the corresponding properties on the C++ General property page for classes.

Override operations

The override modifier in C++ 11 can be used to make it more explicit that a function overrides a virtual function with the same signature in a base class. Model RealTime therefore provides an Override property when defining an operation.

Final classes, capsules and operations

The final modifier in C++ 11 can be used to prevent a class from being inherited, or a function from being overridden. Model RealTime provides a Final property when defining a class, capsule or operation.

Constexpr

A variable marked as constexpr must be initialized at compile time (as opposed to const variables which can be initialized at run-time). Similarly a function that returns a value that is computable at compile time should be marked as constexpr.

Model RealTime supports the constexpr modifier for both attributes and operations by means of a ConstExpr property.

Automatic type deduction

C++ 11 allows a variable to be declared with the auto keyword instead of specifying the type of the variable. Such a variable must have an initializer, and the compiler will deduce the type of the variable from the initializer value. This can be convenient if the type is complex to type (for example contains several template instantiations).

Model RealTime has a primitive type called "auto" in its CppPrimitiveDatatypes package. By using this type you can define an auto variable in the same way as any other variable. Just remember that the C++ variable needs an initializer. This means that the attribute in the model must have a default value, must be static const or static constexpr, and must have the Initializer Kind property set to either Equal or Brace.


This was a brief summary of some of the most important C++ 11 features that you can use with Model RealTime. In a future newsletter we plan to cover the support for language features introduced in newer versions of C++, such as C++ 14 and 17.