cplusplus / CWG

Core Working Group
23 stars 7 forks source link

Can an explicit object member function be called without an class-type object #571

Open ranaanoop opened 1 month ago

ranaanoop commented 1 month ago

Full name of submitter: Anoop Rana

Issue description:

The below program (&w::f)(2) seems to be ill-formed as per the current wording due to omission because there is no class object on which the non-static member function is called. We see implementation divergence here. Both gcc and msvc rejects (&w::f)(0) while clang accepts it.

struct w { 
    constexpr bool f(this int)     
    { 
        return true; 
    } 
    constexpr static bool g(int)
    {
        return true; 
    } 
    const static bool k;
    const static bool p;
}; 
constexpr bool w::k = (&w::g)(0);  //accepts by all as expected
constexpr bool w::p = (&w::f)(0);  //msvc and gcc rejects but clang accepts 
int main()
{ 

}

class.mfct.non.static seems to make this ill-formed because there isn't any class-type object on which the member function f would be called.

A non-static member function may be called for an object of its class type, or for an object of a class derived ([class.derived]) from its class type, using the class member access syntax ([expr.ref], [over.match.call]). A non-static member function may also be called directly using the function call syntax ([expr.call], [over.match.call]) from within its class or a class derived from its class, or a member thereof, as described below.

Suggested resolution:

This can be fixed by making a restriction that the explicit object parameter can only has the same class type(with possible cv qualifier etc). That is, this int should not be possible. Or other option is by removing/changing [class.mfct.non.static#1].

t3nsor commented 1 month ago

As pointed out in the linked Stack Overflow thread, this is just CWG2688.

And furthermore I think we can resolve it in a way that doesn't make this int invalid. We can strike [class.mfct.non.static]/1 which seems to me to be old explanatory text rather than something with a well-defined normative meaning.