Initialize the structure of the template when defining

I'm not sure if the topic matches what I'm looking for, but here it is mostly:

I can do it:

struct something {
    int d;
} somethingType;

But why can't I do this?

template <bool T>
struct somethingelse {
    int d;
}<true> somethingelseType;

If you can do the second, what would be the right way to do this?

+4
source share
3 answers

I think you could, grammatically, but this is forbidden by additional restrictions:

[Tempo] / 1

A template defines a family of classes or functions or an alias for a type family.

  Declaration Template:
    template <List Template Announcement>

  - :
    -
    template-parameter-list , -

[...]

  • -, -, , ,
  • alias.

( ) , .

(.. - ) [dcl.dcl]/1, , decl-specifier-seq [dcl.type]/1, , oO , :

( () )

declaration
block-declaration
simple-declaration
decl-specifier-seqopt                    init-declarator-listopt ;
type-secifier                           init-declarator-listopt ;
class-specifier                         init-declarator-listopt ;
class-head { member-specificationopt }   init-declarator-listopt ;
class-head { member-specificationopt }   init-declarator ;
class-head { member-specificationopt }   declarator initializeropt ;
class-head { member-specificationopt }   ptr-declarator ;
class-head { member-specificationopt }   noptr-declarator ;
class-head { member-specificationopt }   declarator-id attribute-specifier-seqopt ;
class-head { member-specificationopt }   ...opt id-expression ;

, , struct something { int d; } somethingType;

, .

+3

A type-specifiermaybe

  • trailing-type-specifier
  • class-specifier
  • enum-specifier

As you can see, template-declarationit is not included, therefore, what you are looking for does not exist (directly)

See the online version of C ++ 11 grammar rules, hyperlinks: http://www.nongnu.org/hcb/#type-specifier

+1
source

All Articles