Reputation: 5072
Does the C++ standard allow setting attributes on template declarations? For example:
[[attr1]] template <typename T [[attr2]] > [[attr3]]
class [[attr4]] C {};
I believe attr4
would be considered a valid attribute for the template instances (i.e., it would be an attribute on T<int>
and T<char>
, for example).
However would either attr1
or attr3
be considered valid by the compiler (even if it would be ignored)?
Would attr2
also be valid? I think not, but I'm not sure. The issue would be if it "adds" an attribute to a given type used in instantiation, but IIRC currently template parameters have all attributes ignored.
My interest in attr1
and/or attr3
is for a custom test DSL, where a given attribute on a template would automatically force instantiation of the class with a given set of types.
Upvotes: 4
Views: 757
Reputation: 5209
I digged into C++ grammar and here are relevant parts:
declaration:
block-declaration
template-declaration
template-declaration:
template < template-parameter-list > declaration
That forbids [[attr1]]
and [[attr3]]
, if you believe me, that declaration
may not begin with attribute-specifier-seq
.
template-parameter-list:
template-parameter
template-parameter:
type-parameter
type-parameter:
type-parameter-key ..._opt identifier_opt
type-parameter-key:
class
typename
That forbids [[attr2]]
, identifier
in regex is [a-zA-Z_][a-zA-Z_0-9]*
, with universal-character-name
and other implementation-defined characters.
declaration:
block-declaration
simple-declaration:
decl-specifier-seq_opt init-declarator-list_opt ;
decl-specifier-seq:
decl-specifier attribute-specifier-seq_opt
decl-specifier:
type-specifier
type-specifier:
class-specifier
class-specifier:
class-head { member-specification_opt }
class-head:
class-key attribute-specifier-seq_opt class-head-name class-virt-specifier_opt base-clause_opt
That allows [[attr4]]
Upvotes: 5