Modern C++ with Model RealTime

September 02, 2022

In a previous newsletter about the support for C++ 11 in Model RealTime we promised to come back in the future to cover the support for even newer versions of C++. Promises should be kept, so let's take a look at some features of C++ 14, 17 and 20 that now can be used with Model RealTime.


Inline Variables

In C++ 17 global variables and static member variables can be declared as inline. This makes it possible to initialize such a variable in a header file, and then include that header file into multiple compilation units without getting a linker error about multiple defined symbols (as would be the result if the variable was not inline). Note that a constexpr variable also behaves in the same way, but the benefit with inline variables is that they can be initialized with an expression that is not constexpr.

In Model RealTime you can set the property Inline on an attribute in the General tab, or in the Attributes tab of the element that contains the attribute.

Deprecated

A deprecated element is allowed to be used, but its usage is discouraged. Marking elements as deprecated is for example useful when you want to change an API without immediately breaking it. By letting the element you want to change be deprecated for some time, users will get notified if they use that element, and have time to update their code, before you finally choose to remove the deprecated element.

In Model RealTime many elements have a property Deprecated on the C++ General property page.

You can provide a deprecation message using the Message property. The compiler will print that message if it detects that the deprecated element is used. For example:

../DerivedCap.cpp:36:14: warning: 'void DerivedCap_Actor::resizeFields()' is deprecated: Deprecated since version 2.19, use recomputeFields() instead [-Wdeprecated-declarations] resizeFields();

Marking elements as deprecated requires C++ 14 or later. Note that some compilers may require special compile arguments for printing deprecation messages.

Nodiscard

C++ 17 allows functions and their return types to be marked as nodiscard. If the compiler detects that such a function is called, and that the function return value is discarded by the caller, a warning will be printed.

To mark an element as nodiscard in Model RealTime use the Nodiscard property on the C++ General property page. Just like for deprecated elements it's possible to provide a custom warning message using the Message property, but note that this feature requires C++ 20.

C++ Code Standard

To ensure a good user experience, you should tell Model RealTime which version of the C++ language you want to use. You can do so either by means of the preference RealTime Development - Build/Transformations - C++ - C++ code standard or using the transformation configuration property C++ code standard (available in the Code Generation tab of the TC editor). If both these are set, the TC property takes precedence.

The model compiler looks at the specified code standard when it generates C++ code from the model. If it finds that a language construct from a C++ version that is newer than the specified code standard is used, a warning will be printed. If you get such a warning you either should raise the C++ code standard, or remove usage of that language construct. Here is an example of such a warning message:

WARNING : CPPModel::CInline::x : Inline variables require C++ 17 or later

Note that you also need to tell the C++ compiler what version of C++ you want to use. If you don't do this, the compiler will pick a default which is different for different C++ compilers. For example, to set the C++ version to C++ 17 for the Visual Studio compiler you need to provide this compile argument: /std:c++17

You can set the C++ code standard to Older than C++ 11 if your C++ compiler does not support C++ 11. But this requires that you use an older version of the TargetRTS since the latest TargetRTS requires at least C++ 11. The last version of Model RealTime where the TargetRTS could be compiled with something older than C++ 11 is 11.0 2020.22.