5

According to §13.1/2 the two member functions below don't overload. Why is this?

class X {
    static void f();
    void f() const;
};

Edit: the question in NOT a duplicate. The example above shows a const function and a static function who is not const. I know from §13.1/2 that the functions are not overloadable. I just want to know why the Standard doesn't allow the construction given above.

Mao
  • 1,065
  • 8
  • 12
  • 3
    `Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration (9.4)` - ISO 14882:2003 C++ Standard 13.1/2 – Overloadable declarations – Hanky Panky Dec 04 '14 at 10:44
  • 1
    How does the answer in the duplicate not address the "why"? –  Dec 04 '14 at 10:59
  • @remyabel §1.3.20 [defns.signature.member] `signature name, parameter type list (8.3.5), class of which the function is a member, cv-qualifiers (if any), and ref-qualifier (if any).` That is, cv-qualifiers are part of the member function signature. – Mao Dec 04 '14 at 11:08
  • @Chairman `static` is not a *cv-qualifier*. –  Dec 04 '14 at 11:12
  • @remyabel But const is. That's what I said before: the static function is not const, but the other member function is const. Why can't they be overloaded? The compiler will always be able to select the correct function in this case. – Mao Dec 04 '14 at 11:15
  • @Deduplicator Could you tell me, where in the duplicate, is there an example similar to the one I've posted, other than the one in the Standard? – Mao Dec 04 '14 at 11:31
  • @Deduplicator I just want to know why the standard was written this way. I can't see any reason why the two member functions in my example can't be overloaded. Can you explain this? – Mao Dec 04 '14 at 11:41
  • @ChairmanMao: The accepted answer also contains rationale, not only the rule itself. What more would you want? – Deduplicator Dec 04 '14 at 11:44
  • @ChairmanMao which function would compiler choose for `const X x; x.f();` case ? – Piotr Skotnicki Dec 04 '14 at 11:46
  • @PiotrS.The non-static one of course, as the static is not const. – Mao Dec 04 '14 at 11:47
  • @ChairmanMao that's weird reasoning (not to mention the wording *"static is not const"*); and how about `X x; x.f();` then ?? – Piotr Skotnicki Dec 04 '14 at 11:50
  • @PiotrS. That's not wierd. That's what the standard says. In your last question the static is preferred, because it's not const. – Mao Dec 04 '14 at 11:52
  • @ChairmanMao `static` can't be `const` or non-`const`; all the calls would **always** be ambiguous – Piotr Skotnicki Dec 04 '14 at 11:54
  • 2
    @ChairmanMao `cv` qualifiers refer to implicit object parameter that `static` member functions simply do not have, thus they are neither `const` nor non-`const`. non static member function can be overloaded with cv qualifiers, as the compiler knows the cv qualifiers of implicit object parameter at callee side, therefore it can distinguish between overloads by finding the best viable function – Piotr Skotnicki Dec 04 '14 at 11:59
  • @PiotrS. You have already answered my question. I didn't know that static member functions cannot be const qualified. I'll accept this as an answer if the question is reopened. Thanks a lot. – Mao Dec 04 '14 at 12:03

0 Answers0