@@ -991,6 +991,45 @@ To summarize Time declaration and initialization, consider the following example
991991 Time t{100000000}; // NOT OK, is interpreted differently when ``Time::SetResolution() `` called
992992 Time t{0.1}; // NOT OK, will round to zero; see above and also merge request !2007
993993
994+ A constant that cannot be declared as ``constexpr `` (such as an ``ns3::Time `` constant) and needs
995+ to be used in multiple translation units (TUs) shall be declared as ``inline `` and initialized in a
996+ header file that is then included in all the TUs in which the constant needs to be used:
997+
998+ .. sourcecode :: cpp
999+
1000+ inline const Time WIFI_TU = MicroSeconds(1024);
1001+
1002+ Indeed, since C++17, there may be more than one definition of a variable declared as ``inline `` in
1003+ the program as long as each definition appears in a different TU and all definitions are identical.
1004+ See wifi-standard-constants.h for an example of how to group such constants in a header file and
1005+ declare them so that the values may be used in Attribute defaults.
1006+
1007+ This approach is to be preferred over declaring the constant as ``extern `` in a header file and
1008+ initializing the constant in a separate ``.cc `` file, because in such a case it would be dangerous
1009+ to use the constant in the initialization of a static variable (e.g., the ``tid `` static member in
1010+ the static ``GetTypeId `` method of classes inheriting from the ``Object `` class) due to the issue
1011+ of static initialization order.
1012+
1013+ A constant that cannot be declared as ``constexpr `` (such as an ``ns3::Time `` constant) and needs
1014+ to be used in a single TU shall be declared and initialized in the ``.cc `` file before it is used:
1015+
1016+ .. sourcecode :: cpp
1017+
1018+ // Example of declaring a Time constant locally
1019+ // If DEFAULT_BEACON_INTERVAL is declared with the class implementation such as
1020+ // below, and it is used as an an attribute default value, make sure that it
1021+ // is declared before the object TypeId is registered, such as below:
1022+ const Time DEFAULT_BEACON_INTERVAL = MicroSeconds(DEFAULT_BEACON_INTERVAL_USEC);
1023+
1024+ NS_OBJECT_ENSURE_REGISTERED(ApWifiMac);
1025+
1026+ Instead, constants that can be declared as ``constexpr `` (such as all the POD types) shall be
1027+ initialized in an header file by using the ``constexpr `` keyword only:
1028+
1029+ .. sourcecode :: cpp
1030+
1031+ constexpr uint16_t MIN_AID{1};
1032+
9941033Comments
9951034========
9961035
0 commit comments