Declarations[Decl]

7 Declarations[Decl]

7.1 Preamble[Decl.Pre]

Declarations generally specify how names are to be interpreted. Declarations have the form

declaration-seq:
declaration
declaration-seq declaration
declaration:
name-declaration
special-declaration
name-declaration:
block-declaration
function-definition
template-declaration
namespace-definition
empty-declaration
attribute-declaration
cbuffer-declaration
special-declaration:
export-declaration-group
cbuffer-member-declaration
block-declaration:
simple-declaration
namespace-alias-definition
using-declaration
using-directive
static_assert-declaration
alias-declaration
opaque-enum-declaration
empty-declaration:
;

7.2 Specifiers[Decl.Spec]

7.2.1 General[Decl.Spec.General]

The specifiers that can be used in a declaration are

decl-specifier:
function-specifier
...

7.2.2 Function specifiers[Decl.Spec.Fct]

A function-specifier can be used only in a function declaration.

function-specifier:
export

The export specifier denotes that the function has program linkage (3.7.1).

The export specifier cannot be used on functions directly or indirectly within an unnamed namespace.

Functions with program linkage can also be specified in export-declaration-group (7.7).

If a function is declared with an export specifier then all redeclarations of the same function must also use the export specifier or be part of export-declaration-group (7.7).

7.3 Declarators[Decl.Decl]

7.4 Initializers[Decl.Init]

The process of initialization described in this section applies to all initializers regardless of the context.

initializer:
brace-or-equal-initializer
( expression-list )
brace-or-equal-initializer:
= initializer-clause
braced-init-list
initializer-clause:
assignment-expression
braced-init-list
braced-init-list:
{ initializer-list ,opt }
{ }
initializer-list:
initializer-clause
initializer-list , initializer-clause

7.4.1 Aggregate Initialization[Decl.Init.Agg]

An aggregate is a vector, matrix, array, or class.

The subobjects of an aggregate have a defined order. For vectors and arrays the order is increasing subscript order. For matrices it is increasing subscript order with the subscript nesting such that in the notation Mat[M][N], the ordering is Mat[0][0]...Mat[0][N]...Mat[M][0]...Mat[M][N]. For classes the order is base class, followed by member subobjects in declaration order.

A flattened ordering of subobjects can be produced by performing a depth-first traversal of the subobjects of an object following the defined subobject ordering.

Each braced initializer list is comprised of zero or more initializer-clause expressions, which is either another braced initializer list or an expression which generates a value that either is or can be implicitly converted to an rvalue. Each assignment-expression is an object, which may be a scalar or aggregate type. A flattened initializer sequence is a sequence of expressions constructed by a depth-first traversal over each assignment-expression in an initializer-list and performing a depth-first traversal accessing each subobject of the assignment-expression.

If the target object is an array of unknown size, the object is assumed to have m possible elements during parsing, where m > 0.

An initializer-list is a valid initializer if for each element En mod  m in the target object’s flattened ordering there is a corresponding expression En in the flattened initializer sequence, which can be implicitly converted to the element’s type. For arrays of unknown size, the total number of expressions in the flattened initializer sequence must be a multiple of the array’s base element type.

An initializer-list is invalid if the flattened initializer sequence contains more or fewer elements than the target object’s flattened ordering, or if any initializer In cannot be implicitly converted to the corresponding element En’s type.

7.5 Function Definitions[Decl.Function]

7.6 Attributes[Decl.Attr]

7.6.1 Semantic Annotations[Decl.Attr.Semantic]

7.6.2 Entry Attributes[Decl.Attr.Entry]

7.7 Export Declarations[Decl.Export]

One or more functions with external linkage can be also specified in the form of

export-declaration-group:
export { function-declaration-seqopt }
function-declaration-seq:
function-declaration function-declaration-seqopt

The export specifier denotes that every function-declaration included in function-declaration-seq has external linkage (3.7.2).

The export-declaration-group declaration cannot appear directly or indirectly within an unnamed namespace.

Functions with external linkage can also be declared with an export specifier (7.2.2).

If a function is part of an export-declaration-group then all redeclarations of the same function must also be part on a export-declaration-group or be declared with an export specifier (7.2.2).

7.8 Constant Buffer Declarations[Decl.cbuffer]

cbuffer-declaration:
cbuffer name resource-bindingopt { cbuffer-member-seqopt }
cbuffer-member-seq:
cbuffer-member-declaration
cbuffer-member-seq cbuffer-member-declaration
cbuffer-member-declaration:
block-declaration
function-definition
template-declaration
empty-declaration

A cbuffer declaration is declared with the cbuffer keyword. The name of the cbuffer declaration does not declare a name, and cannot be referenced from within the translation unit, nor is it required to be unique. Each cbuffer declaration refers to a unique constant buffer resource (9.5).

The cbuffer declaration itself does not declare a declaration scope. Declarations within a cbuffer declaration that declare names, declare their names in the scope containing the cbuffer declaration. A cbuffer declaration may not contain a namespace-declaration or cbuffer-declaration. 1

Variable declarations with program storage duration (3.6.3) in the cbuffer declaration are called shader constants. Shader constants are implicitly const and cannot be modified in program code.2