3.5.4. Read/write constants

A linkage specification for external constants indicates that a constant can be dynamically initialized or have mutable members.

Note

The use of "C++:read/write" linkage is only necessary for code compiled with --apcs /rwpi. If you recompile existing code with this option, you must change the linkage specification for external constants that are dynamically initialized or have mutable members.

Compiling C++ with the --apcs /rwpi option deviates from the ISO C++ Standard. The declarations in Example 3.1 assume that x is in a read-only segment.

Example 3.1. External access

extern const T x;
extern "C++" const T x;
extern "C" const T x;

Dynamic initialization of x including user-defined constructors is not possible for constants and T cannot contain mutable members. The new linkage specification in Example 3.2 declares that x is in a read/write segment even if it is initialized with a constant. Dynamic initialization of x is permitted and T can contain mutable members. The definitions of x, y, and z in another file must have the same linkage specifications.

Example 3.2. Linkage specification

extern const int z;                   /* in read-only segment, cannot  */	
                                      /* be dynamically initialized    */	

extern "C++:read/write" const int y;  /* in read/write segment */	
                                      /* can be dynamically initialized */	
extern "C++:read/write"
{	
    const int i=5;                    /* placed in read-only segment, */	
                                      /* not extern because implicitly static */	
    extern const T x=6;               /* placed in read/write segment */	
    struct S
    {	
        static const T T x;           /* placed in read/write segment */	
    };	
}

Constant objects must not be redeclared with another linkage. The code in Example 3.3 produces a compile error.

Example 3.3. Compiler error

extern "C++"  const T x;
extern "C++:read/write"  const T x; /* error */

Note

Because C does not have the linkage specifications, you cannot use a const object declared in C++ as extern "C++:read/write" from C.

See also

Copyright © 2007, 2010 ARM Limited. All rights reserved.ARM DUI 0348A
Non-Confidential