Go to main content
Oracle® Developer Studio 12.6: C++ User's Guide

Exit Print View

Updated: July 2017

4.11 Supported Attributes

The following are supported attributes:The following attributes, invoked by __attribute__ ((keyword)), or alternatively by [[keyword]], are implemented by the compiler for compatibility. Spelling the attribute keyword within double underscores, __keyword__, is also accepted.


Roughly equivalent to #pragma align. Generates a warning and is ignored if used on variable length arrays.


Equivalent to #pragma inline and -xinline


Equivalent to #pragma no_side_effect


Equivalent to #pragma init


Results in a warning if the type, variable, or function is used in the source file. The optional argument msg must be a string literal and will be included in the warning message.


Equivalent to #pragma fini


Equivalent to #pragma returns_new_memory


(No equivalent)


Equivalent to #pragma no_inline and -xinline


Equivalent to #pragma does_not_return


Equivalent to #pragma does_not_write_global_data


Equivalent to #pragma pack(). For more information, see #pragma pack(n).


Equivalent to #pragma unknown_control_flow

section (section-name)

Places a global variable (or function) in a named section, instead of the sections .bss or .data. Make sure to initialize any global variable placed in a named section to avoid multiply defined error messages from the linker.


Accepted for compatibility with g++, but has no effect. The g++ documentation recommends not using this attribute.


Sets the thread-local storage model. The tls_model argument should be one of global-dynamic, local-dynamic, initial-exec, or local-exec.


Indicates that a variable or a type name (created using typedef) represents a vector.


Provides linker scoping. (See -xldscope={v}) Syntax is: __attribute__((visibility(“visibility-type”))), where visibility-type is one of:


Same as __global linker scoping


Same as __hidden linker scoping


Same as __symbolic linker scoping


Equivalent to #pragma weak

4.11.1 __has_attribute function-like macro

The predefined function-like macro


evaluates to 1 if attr is a supported attribute. It evaluates to 0 otherwise. Example usage:

#ifndef __has_attribute // if we don't have __has_attribute, ignore it
    #define __has_attribute(x) 0
#if __has_attribute(deprecated)
   #define DEPRECATED __attribute__((deprecated))
   #define DEPRECATED // attribute "deprecated" not available
void DEPRECATED old_func(int); // use the attribute if available

4.11.2 __packed__ Attribute Details

This attribute, attached to struct or union type definition, specifies that each member (other than zero-width bit-fields) of the structure or union is placed to minimize the memory required. When attached to an enum definition, __packed__ indicates that the smallest integral type should be used.

Specifying this attribute for struct and union types is equivalent to specifying the packed attribute on each of the structure or union members.

In the following example, struct my_packed_struct's members are packed closely together but the internal layout of its s member is not packed. To do that, struct my_unpacked_struct would also need to be packed.

struct my_unpacked_struct
   char c;
   int i;
struct __attribute__ ((__packed__)) my_packed_struct
   char c;
   int  i;
   struct my_unpacked_struct s;

You can only specify this attribute on the definition of an enum, struct, or union, and not on a typedef that does not also define the enumerated type, structure, or union.