using vs. typedef - is there a subtle, lesser known difference?

后端 未结 2 1757
小鲜肉
小鲜肉 2021-01-01 12:02

Background

Everybody agrees that

using  = ;

is equivalent to

typedef          


        
2条回答
  •  春和景丽
    2021-01-01 12:22

    It looks like the standard is unclear on this.

    On one hand,

    [dcl.typedef] A typedef-name can also be introduced by an alias-declaration. [...] Such a typedef-name has the same semantics as if it were introduced by the typedef specifier.

    On the other hand, the standard clearly separates the notions of typedef declaration and alias-declaration (the latter term is a grammar production name, so it is italicised and hyphenated; the former is not). In some contexts it talks about "a typedef declaration or alias-declaration", making them equivalent in these contexts; and sometimes it talks solely about "a typedef declaration". In particular, whenever the standard talks about linkage and typedef declarations, it only talks about typedef declarations and does not mention alias-declaration. This includes the key passage

    [dcl.typedef] If the typedef declaration defines an unnamed class (or enum), the first typedef-name declared by the declaration to be that class type (or enum type) is used to denote the class type (or enum type) for linkage purposes only.

    Note the standard insists on the first typedef-name being used for linkage. This means that in

    typedef struct { int x; } A, B;
    

    only A is used for linkage, and B is not. Nothing in the standard indicates that a name introduced by alias-declaration should behave like A and not like B.

    It is my opinion that the standard is insufficiently clear in this area. If the intent is to make only typedef declaration work for linkage, then it would be appropriate to state explicitly in [dcl.typedef] that alias-declaration does not. If the intent is to make alias-declaration work for linkage, this should be stated explicitly too, as is done in other contexts.

提交回复
热议问题