Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6052965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:59:12+00:00 2026-05-23T07:59:12+00:00

I was quite surprised when I saw the following code compile without errors or

  • 0

I was quite surprised when I saw the following code compile without errors or warnings in g++-4.2:

typedef enum test { one };

My assumption was that if you used the typedef keyword it would require an extra identifier as in:

typedef enum test { one } test;

As already mentioned, g++-4.2 accepts it without even a warning. Clang++ 3.0 warns “warning: typedef requires a name“, similarly Comeau warns “warning: declaration requires a typedef name“, and g++-4.6 informs: “warning: ‘typedef’ was ignored in this declaration“.

I have not been able to identify where in the standard this is allowed, and I find it slightly confusing that two of the compilers warn that it is required, shouldn’t it be an error if the typedef-name is required but not present?

UPDATE: I have checked in C with the same compilers. Clang and comeau yield the same output, gcc gives a warning: “warning: useless storage class specifier in empty declaration“, which seems even more confusing.

UPDATE: I have checked removing the name of the enum and the results are the same:

typedef enum { one };

Similarly with a named struct:

typedef struct named { int x };

But not with an unnamed struct, in which case the code was rejected in g++ (4.2/4.6) with “error: missing type-name in typedef-declaration“, gcc (4.2/4.6) gave a warning: “warning: unnamed struct/union that defines no instances“, clang++ “warning: declaration does not declare anything“, comeau “error: declaration requires a typedef name“

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:59:13+00:00Added an answer on May 23, 2026 at 7:59 am

    It is a degenerate syntax that is allowed but provides no benefit. Most modern compilers can be provoked into emitting a warning about it; by default, they may not. Without the typedef name, the keyword typedef is superfluous; in your example, it is completely equivalent to:

    enum test { one };
    

    Another place where it can occur is with a structure:

    typedef struct SomeThing { int whatever; };
    

    This is equivalent to:

    struct SomeThing { int whatever; };
    

    Note that typedef is officially (or syntactically) a ‘storage class specifier’, like static, extern, auto and register.


    C Standard

    In ISO/IEC 9899:1999 (that’s the C standard), we find:

    §6.7 Declarations

    Syntax

    declaration:

    declaration-specifiers init-declarator-listopt;

    declaration-specifiers:

    storage-class-specifier declaration-specifiersopt

    type-specifier declaration-specifiersopt

    type-qualifier declaration-specifiersopt

    function-specifier declaration-specifiersopt

    init-declarator-list:

    init-declarator

    init-declarator-list , init-declarator

    init-declarator:

    declarator

    declarator = initializer

    And (as requested):

    §6.7.1 Storage-class specifiers

    Syntax

    storage-class-specifier:

    typedef

    extern

    static

    auto

    register

    If you track through that syntax, there are a lot of degenerate possibilities, and what you showed is just one of the many.


    C++ Standard

    It is possible that C++ has different rules.

    In ISO/IEC 14882:1998 (the original C++ standard), we find in §7.1.1 ‘Storage class specifiers’ that C++ does not treat typedef as a storage class; the list adds mutable and excludes typedef. So, the grammatical specification of typedef in C++ is definitely different from the C specification.

    §7 Declarations

    Declarations specify how names are to be interpreted. Declarations have the form

    declaration-seq:

    declaration

    declaration-seq declaration

    declaration:

    block-declaration

    function-definition

    template-declaration

    explicit-instantiation

    explicit-specialization

    linkage-specification

    namespace-definition

    block-declaration:

    simple-declaration

    asm-definition

    namespace-alias-definition

    using-declaration

    using-directive

    simple-declaration:

    decl-specifier-seqopt init-declarator-listopt ;

    …

    ¶5 If the decl-specifier-seq
    contains the typedef specifier, the declaration is called a typedef declaration and
    the name of each init-declarator
    is declared to be a typedef-name,
    synonymous with its associated type
    (7.1.3).

    §7.1 Specifiers [dcl.spec]

    The specifiers that can be used in a declaration are

    decl-specifier:

    storage-class-specifier

    type-specifier

    function-specifier

    friend

    typedef

    decl-specifier-seq:

    decl-specifier-seqopt

    decl-specifier

    §7.1.1 Storage class specifiers [dcl.stc]

    storage-class-specifier:

    auto

    register

    static

    extern

    mutable

    §7.1.2 Function specifiers [dcl.fct.spec]

    function-specifier:

    inline

    virtual

    explicit

    §7.1.3 The typedef specifier [dcl.typedef]

    Declarations containing the decl-specifier
    typedef declare identifiers that can be used later for naming
    fundamental (3.9.1) or compound (3.9.2) types. The typedef specifier shall not be used in a function-definition
    (8.4), and it shall not be combined in a decl-specifier-seq
    with any other kind of specifier except
    a type-specifier.

    typedef-name:

    identifier

    …

    In a given scope, a typedef specifier can be used to redefine the name of any type declared in that scope
    to refer to the type to which it already refers. [Example:

    typedef struct s { /* ... */ } s;
    typedef int I;
    typedef int I;
    typedef I I;
    

    —end example]

    §7.1.4 The friend specifier [dcl.friend]

    The friend specifier is used to specify access to class members; see 11.4.

    §7.1.5 Type specifiers [dcl.type]

    type-specifier:

    simple-type-specifier

    class-specifier

    enum-specifier

    elaborated-type-specifier

    cv-qualifier


    Since §7 ¶5 says that typedef names come from the init-declarator and the init-declarator-list is tagged ‘opt‘, I think that means that the typedef name can be omitted in C++, just as in C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite surprised when I compile the following code without any warning using g++
I was quite surprised of the output of the following code: Country class public
After years of ASP.NET development I'm actually quite surprised that I can't seem to
I'm new to JScript coming from a C++ world. I'm quite surprised that expressions
When trying to compile the following code in LINQPad : void Main() { DriveInfo.GetDrives().Select(GetProviderName).Dump();
I was quite surprised to find inconsistent results today from the following MySQL query:
I installed debian lenny, but I was quite surprised that I can't get man
I was quite surprised to find that the keys function happily works with arrays:
I'm quite surprised that Google didn't find a solution. I'm searching for a solution
I'm quite surprised I haven't been able to find out what characters I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.