Question

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

I can do this:

struct something {
    int d;
} somethingType;

But why can't I do this?

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

If it is possible to do the second one, what will be the correct way of doing it?

Was it helpful?

Solution

I think you could, grammatically, but it's forbidden by additional restrictions:

[temp]/1

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

   template-declaration:
     template < template-parameter-list > declaration

   template-parameter-list:
     template-parameter
     template-parameter-list , template-parameter

[...]

The declaration in a template-declaration shall

  • declare or define a function or a class, or
  • define a member function, a member class, a member enumeration, or a static data member of a class template or of a class nested within a class template, or
  • define a member template of a class or class template, or
  • be an alias-declaration.

A declaration (the top-level grammatical construct) can be a template-declaration, so these rules directly apply to the declaration.

On the other hand (i.e. for non-template classes), a declaration can also be a block-declaration [dcl.dcl]/1, which can be a simple-declaration, which can contain a decl-specifier-seq which can contain a type-specifier [dcl.type]/1 which can be a class-specifier which can declare a class o.O and using this type-specifier you could declare a variable:

(resolving one construct per line into its (possible) components)

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 ;

Which now matches, say, struct something { int d; } somethingType;

But that would be a declaration of a variable, which is not allowed for the template case.

OTHER TIPS

I don't think you can.

You can follow a struct by object names (according to http://www.cplusplus.com/doc/tutorial/structures/), not by template parameters.

A type-specifier can be

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

As you can see, template-declaration is not included, hence what you are looking for doesn't exist (directly)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top