Search code examples
c++ciarcortex-mcmsis

Is there any reason to declare something "volatile const" in C but only "volatile" in C++?


I was using a header file in my project that had the following define(s):

#ifdef __cplusplus
  extern "C" {
#endif 

#ifdef __cplusplus
  #define   __I     volatile             /*!< Defines 'read only' permissions*/
#else
  #define   __I     volatile const       /*!< Defines 'read only' permissions*/
#endif

The __I is used as follows in another header file:

    typedef struct {   
    // more members before         
      __I  uint32_t  CR;   /*!< GPIO Commit*/
    // more members after

    } GPIOA_Type;

#define GPIOF_BASE                      0x40025000UL
#define GPIOF                           ((GPIOA_Type *) GPIOF_BASE)

My question is why would the __I be made const in C but not in C++? You can still modify the value CR is pointing to since you have the address, but am just curios why the definition of __I is different.

For anybody interested what this is for or from, the __I defines are from IAR Embedded Workbench ARM for Cortex-M4 , and the struct is from Texas Instruments LM4F120H5QR CMSIS files.


Solution

  • In C++, const variables at file scope default to static linkage, which wouldn't be desired for memory-mapped GPIOs. The "right" fix for that is the extern keyword, but that can't be used here, since evidently __I needs to work with class members as well. So eliminating const makes the default linkage extern, as desired.