Usually, we do declare but not define a global variable in the header file. However, we define templates in it. Then the issue arises: is it possible to define a global variable template?
template <uint8_t PinCode>
uint8_t BitMask = digitalPinToBitMask(PinCode);
In reality, a global variable template:
- instantizes only if at least one compile unit (CU) requires it.
- causes no redefinition errors if multiple CUs require the same instance.
- is shared among CUs, i.e., if one CU changes the value of an instance, other CUs requiring the same instance will be affected.
- calls the initializer only once for each kind of required instance.
The standard defines many global constants which are templatized such as the type traits
type_trait_vconstants.They are defined as such in the header
In your example you could do something like
For this to work then
digitalPinToMaskmust be invocable in aconstexprcontext.