Repeated typedefs - invalid in C but valid in C++?

后端 未结 5 760
借酒劲吻你
借酒劲吻你 2020-11-28 09:58

I would like a standard reference why the following code triggers a compliance warning in C (tested with gcc -pedantic; \"typedef redefinition\"), but is fine i

5条回答
  •  攒了一身酷
    2020-11-28 10:12

    You can do it in C++ because of 7.1.3/3 and /4.

    You can't do it in C99 because it doesn't have any equivalent special case in 6.7.7, so re-declaring a typedef name follows the same rules as re-declaring any other identifier. Specifically 6.2.2/6 (typedefs have no linkage) and 6.7/3 (identifiers with no linkage can only be declared once with the same scope).

    Remember typedef is a storage-class-specifier in C99, whereas in C++ it's a decl-specifier. The different grammar leads me to suspect that the C++ authors decided to put more effort into making typedefs "a different kind of declaration", and so may well have been willing to spend more time and text on special rules for them. Beyond that I don't know what the C99 authors' (lack of) motivation was.

    [Edit: see Johannes's answer for C1x. I'm not following that at all, so I should probably stop using "C" to mean "C99" because I probably won't even notice when they ratify and publish. It's bad enough as it is: "C" should mean "C99", but in practice means "C99 if you're lucky, but if you have to support MSVC then C89".]

    [Edit again: and indeed, it has been published and is now C11. Woot.]

提交回复
热议问题