Skip to content

CWG3160 [dcl.spec][class.ctor] Grammar ambiguity related to constructor declarations #741

Description

@cpplearner

Full name of submitter (unless configured in github; will be published with the issue): Tam S. B.

Reference (section label): [dcl.spec] [class.ctor]

Link to reflector thread (if any):

Issue description:

Consider

typedef int t1, t2, t3, t4;

struct A {
    static A(*t1)(); // #1
    static A(t2)();  // #2
    static A(t3);    // #3
    A(t4);           // #4
};

It's common sense that #1 is a function pointer and #4 is a constructor, but the interpretation of #2 and #3 seems ambiguous: they could be function/variable declarations with redundant parentheses, or constructors with invalid static specifiers.

All compilers seem to choose the second interpretation, parsing #2 and #3 as invalid constructor declarations. However, they disagree with each other if static gets replaced with typedef or friend:

typedef int t5, t6, t7;

struct A {
    typedef A(t5)();  // #5
    typedef A(t6);    // #6
};

namespace ns { struct A {
    friend A(t7)();    // #7
}; }

GCC and EDG seem to parse #5 and #6 as valid typedef declarations, and EDG seems to parse #7 as a valid friend declaration. Clang and MSVC parse them as invalid constructor declarations.

MSVC also considers A static (t3); to be a constructor declaration, but no one else does.

Suggested resolution:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions