Bug 41575 - GCC lists private methods as candidates in "no matching function for call"
Summary: GCC lists private methods as candidates in "no matching function for call"
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2009-10-05 10:20 UTC by Martin Olsson
Modified: 2017-02-09 00:19 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-02-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Olsson 2009-10-05 10:20:48 UTC
The following C++ code:
########################################

class Klass
{
public:
private:
        static void f(const char*& a, int b) {}
};

int main (void)
{
        char* x;
        Klass::f (x);
}


########################################
Incorrectly produces the follow error message in GCC:
main.cpp: In function ‘int main()’:
main.cpp:13: error: no matching function for call to ‘Klass::f(char*&)’
main.cpp:7: note: candidates are: static void Klass::f(const char*&, int)


This is not correct because f() is not a valid candidate because it's private and can never be called from outside of Klass.

The purpose of this error message is to help the end user understand why his function call is not accepted by GCC (usually it's because one of the types are wrong in the parameter list, which is why the provided and candidate parameters are printed in the first place). However, in this context the fact that f is private is also highly relevant.

Basically this is very confusing, GCC should mention that the only candidate is private and that's why stuff won't compile. Alternatively, GCC should not claim that f() is a candidate at all, because in reality it's not a candidate because it's not callable.
Comment 1 Jonathan Wakely 2009-10-05 10:28:49 UTC
Access checking happens after overload resolution.
Comment 2 Andrew Pinski 2009-10-05 15:14:37 UTC
Namelookup is done before access control IIRC.
Comment 3 Martin Sebor 2017-02-07 19:05:58 UTC
As stated in the comments the error message is strictly correct because overload resolution takes place before access checking.  At the same time, I do agree that it might be helpful to mention the access level of each candidate in the error message if/when the access matters, similarly to what Clang does (below). On that basis I'll confirm this as an enhancement request.

t.C:11:16: error: 'f' is a private member of 'Klass'
        Klass::f (x);
               ^
t.C:5:21: note: declared private here
        static void f(const char*& a, int b) {}
                    ^
t.C:11:20: error: too few arguments to function call, expected 2, have 1
        Klass::f (x);
        ~~~~~~~~   ^
t.C:5:9: note: 'f' declared here
        static void f(const char*& a, int b) {}