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

Exit Print View

Updated: July 2016
 
 

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.

aligned

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

always_inline

Equivalent to #pragma inline and -xinline

const

Equivalent to #pragma no_side_effect

constructor

Equivalent to #pragma init

deprecated(msg)

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.

destructor

Equivalent to #pragma fini

malloc

Equivalent to #pragma returns_new_memory

mode

(No equivalent)

noinline

Equivalent to #pragma no_inline and -xinline

noreturn

Equivalent to #pragma does_not_return

pure

Equivalent to #pragma does_not_write_global_data

packed

Equivalent to #pragma pack(). See details below.

returns_twice

Equivalent to #pragma unknown_control_flow

strong

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

tls_model

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

vector_size

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

visibility

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

default

Same as __global linker scoping

hidden

Same as __hidden linker scoping

internal

Same as __symbolic linker scoping

weak

Equivalent to #pragma weak

4.11.1 __has_attribute function-like macro

The predefined function-like macro

__has_attribute(attr)

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
#endif
#if __has_attribute(deprecated)
   #define DEPRECATED __attribute__((deprecated))
#else
   #define DEPRECATED // attribute "deprecated" not available
#endif
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 may 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.