Why are typedef patterns prohibited?

From a practical point of view, I understand that both typedef and test are somewhat "redundant" and they need to be removed if we want the following code to compile:

 template< typename type_t > typedef struct tagTest { int a; } test; 

However, I thought the typedef declaration set was a subset of the declaration set. They just got this specific qualifier. It was my rationalization for

 typedef struct tagTest { int a; } test; 

by entering the identifier test and declaring the tagTest structure. If this interpretation is correct, then the next paragraph from the standard should allow a template typedef (although not with the value specified by the using keyword).

The declaration in the template declaration shall - (1.1) declare or define a function, class or variable, or - (1.2) define a member function, member class, element enumeration or static data member of a class template or class nested in a class template, or (1.3) define a member template of a class or class template, or - (1.4) be a declaration of aliases.

I do not see a mistake in my reasoning, but the conclusion is illegal.

What are the relevant parts of the standard that solve the above puzzle?


UPDATE The above part uses the fact that typedef struct declares a structure. The typedef , as I understand it, implies that any declared variables are indeed types. That is, typedef updates test from a simple variable to a type equivalent to the declared tagTest . This is why the following code compiles (albeit with a warning).

 typedef struct tagTest { int a; }; tagTest t; 

One of the answers is responsible for the extra test . But you can use typedef without a declarator because "Init-declarator-list is optional when declaring a named class / structure / union or named enumeration"

+6
source share
3 answers

Typedefs are not permitted by pre-C ++ 11 and with C ++ 11 template aliases to solve these problems. CFR. C ++ template typedefs and wikipedia .

Since, as you noted, the standard does not allow typedef be there, the code is invalid

declaration alias:

  using identifier attribute-specifier-seqopt= type-id ; 

typedef declarations are not alias declarations.

In addition, you cannot have a declarator, if you declare a class template, it is explicitly forbidden by the standard

[Temp] / p3

In a template declaration, explicit specialization, or explicit instance of init-declarator-list, the declaration must contain no more than one declarator. When such an declaration is used to declare a class template, the declarator is not permitted .

therefore the following will not even compile

 template< typename type_t > struct tagTest { int a; } test; 

Edit:

Nowhere is it indicated that

 typedef struct S { }; 

there should be an error, so gcc and clang accept it with a warning. I believe that Clang relies on [temp] / 3 to throw an error when typedef was used with the template, and gcc immediately rejected this code.

 template<typename T> typedef struct S { }; 

CFR. clang bug 22249

+3
source

Regardless of what typedef defines, that is, a typedef declaration that is not specified in these cases:

  • [member] function
  • [member] class
  • variable
  • transfer of participants
  • static data element of a class template / class nested in a class template
  • class or class template element template
  • Nickname Declaration

And just to be clear, typedef declarations are not alias declarations. The declaration of the alias indicated in the grammar in Β§ 7 of the standard:

declaration alias:

using identifier attribute-specifier-seqopt= type-id ;

Not to mention the fact that if it was possible, then declaring the using template would not be nearly as β€œcool” as it is today, but it would make little sense for and .

+1
source

C does not support patterns and

 typedef struct tagX { } X; 
Syntax

in C ++ it is rudimentary C to provide constant support for C headers, etc., and not for use in real C ++.

C ++ syntax for the above

 struct X {}; 

(YMMV when placing brackets)

+1
source

Source: https://habr.com/ru/post/989180/


All Articles