cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[class.mem#general] Constructors considered non static member function? #502

Closed ranaanoop closed 4 months ago

ranaanoop commented 4 months ago

Full name of submitter: Anoop Rana

Reference (section label): [class.mem#general]

Link to reflector thread (if any): https://stackoverflow.com/questions/77968375/is-a-constructor-that-is-not-a-special-member-function-still-a-member-function

Issue description:

While answering this question on stackoverflow which basically wanted to know if all ctor are member functions, I noticed a possible defect afaik. In particular, I noted that [class.mem#general] says that:

A data member or member function may be declared static in its member-declaration, in which case it is a static member (see [class.static]) (a static data member ([class.static.data]) or static member function ([class.static.mfct]), respectively) of the class. Any other data member or member function is a non-static member (a non-static data member or non-static member function ([class.mfct.non.static]), respectively).

And since a constructor is a member function(as explained here), the above seems to imply that a constructor is also considered to be a non-static member. So is this the intention(that is, is a ctor intended to be considered a non-static member).

Suggested resolution

Change [class.mem#general] to say:

A data member or member function may be declared static in its member-declaration, in which case it is a static member (see [class.static]) (a static data member ([class.static.data]) or static member function ([class.static.mfct]), respectively) of the class. Any other data member or member function (except constructors and destructors) is a non-static member (a non-static data member or non-static member function ([class.mfct.non.static]), respectively).

Note the added exception for constructors and destructors in the above modified/updated/suggested resolution.

jensmaurer commented 4 months ago

I think the status quo is fine; we want ctors and dtors to be non-static member functions so that "this" works there. (There are other restrictions that prevent calls to e.g. constructors using class member access syntax.)

ranaanoop commented 4 months ago

@jensmaurer I see. I was not aware(until I read the standard) that ctor are non-static member functions as most books that I've read did not mention this.