19

I recently find out that there is a reference-to-function concept in C++ :). So as there are pointer-to-function and pointer-to-member-function different types. The question arises. Is there a "reference-to-member-function" concept?

I tried to compile the following code, but GCC 3.4.6 gives an error.

#include <iostream>

using namespace std;

class A {
public:
  virtual void Af() const {
    cout << "A::Af()" << endl;
  }
};

int main() {
  typedef void (A::& MemFnc)() const;
  MemFnc mf = &A::Af;

  A a;
  (a.*mf)();

  return 0;
}
MKo
  • 4,768
  • 8
  • 32
  • 35
  • 3
    gcc 3.4.6? You should consider updating the compiler. Also, when asking about code that triggers compiler errors, it is usually a good idea to provide the actual error message (starting from the first error in the output) and identify the line where the compiler complains. – David Rodríguez - dribeas Aug 30 '11 at 15:12
  • I have no permission to update GCC :(. I didn't provide the error code because I think in this case it doesn't matter what type of error it gives, the essential part is it doesn't understand this syntax. – MKo Aug 30 '11 at 15:27
  • If there would be a reference to member, which syntax should be used to use it? A pointer-to-member is used with `.*`, so common usage suggests that reference to member should be just dropping the star, which turns out to be plain `.`?! Which is exactly the same as using an ordinary member. – Sjoerd Aug 30 '11 at 15:38
  • Oh! I now understand what you were asking. This is not the problem I was having. I was trying to do reference to member function operator (.*). You are trying to declare a member function reference. Just note that function references are just a novelty. There is no difference between a function reference and function pointer. So that is pointless use of your mental capabilities. – user13947194 Jun 09 '22 at 06:31

3 Answers3

25

There is no such a thing called reference to member in C++.

The language specification explicitly says in a note (§8.3.3/3 - 2003) that,

A pointer to member shall not point to a static member of a class (9.4), a member with reference type, or “cv void.” [Note: see also 5.3 and 5.5. The type “pointer to member” is distinct from the type “pointer”, that is, a pointer to member is declared only by the pointer to member declarator syntax, and never by the pointer declarator syntax. There is no “reference-to-member” type in C++.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 3
    Technically, what you're highlighted in **bold** is non-normative as it is just a note. – CB Bailey Aug 30 '11 at 15:22
  • 1
    @Charles: Yeah. I know that. But that is easy to read and understand. That is why I quoted it. – Nawaz Aug 30 '11 at 15:25
  • 9
    My point is the when you say "The language specification explicitly says", it is a little disingenuous because the specification doesn't explicitly say 'there is no "reference-to-member" type', it just omits to provide one. – CB Bailey Aug 30 '11 at 15:29
  • @Charles: Alright. Let me edit my post. :-) – Nawaz Aug 30 '11 at 15:30
  • @Charles: Is it appropriate now? – Nawaz Aug 30 '11 at 15:32
  • Thanks. P.S. I tried to find "reference-to-member-function" string in C++ standard specification, but without any result. the "function" word was excessive :) – MKo Aug 30 '11 at 15:32
  • 3
    @Charles: but notes are described as "informative". Therefore they must be true, otherwise they wouldn't be informative, they'd be misinformative ;-) It's only where there's fine hair-splitting or inconsistent use of language that you need to use the fact that notes are subsidiary to the main paragraphs of the standard. They're still part of the standard, and so what they say the standard says. It just says it informatively rather than normatively. The standard explicitly *says* this, but I suppose it doesn't explicitly *define* it, only implicitly defining it. – Steve Jessop Aug 30 '11 at 18:14
  • 3
    @SteveJessop: I wasn't objecting the quotation which is useful (if not definitive), just the prefix of "The language specification explicitly says" without any qualification which I felt bordered on the misleading. The informative notes are not part of the language specification itself, just an aid to interpretation. – CB Bailey Aug 30 '11 at 20:45
  • The reason why they don't exist is given [here](http://stackoverflow.com/a/21952738/616460). – Jason C Feb 22 '14 at 17:41
2

No, references to member functions are not possible.

In some sense, the result of dereferencing a pointer to a member function could serve as one, but the only thing you can do with that result is to invoke a function call operator on it, per 5.5[expr.mptr.oper]/6. Nothing else is allowed.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
1

There is no reference to member function.

Tom Kerr
  • 10,444
  • 2
  • 30
  • 46