13

I've always believed that function pointers don't require an ampersand:

Do function pointers need an ampersand

Yet, every example I've seen of using boost::bind shows one, and my compiler - in most situations - gives a typically inscrutable error message if it's omitted.

synchronize(boost::bind(&Device::asyncUpdate , this, "ErrorMessage"));  // Works

synchronize(boost::bind(Device::asyncUpdate , this, "ErrorMessage"));   // Fails

Am I wrong in assuming that boost::bind's first parameter is basically function pointer?

Community
  • 1
  • 1
Roddy
  • 66,617
  • 42
  • 165
  • 277
  • 1
    Looks like a compiler bug, friendship shouldn't make any difference, the & is required for forming a pointer to member. – Jonathan Wakely Aug 19 '13 at 12:01
  • 2
    Note that `static` members functions behave like free functions w.r.t function pointers. They can't bind to member pointers and you can omit `&`. – jrok Aug 19 '13 at 12:03

1 Answers1

22

Function pointers don't need it, member function pointers do.

Device::asyncUpdate is member function, as you could guess because it is being bound to this.

Here's a normative quote from n3337, 5.3.1/4

A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • @Roddy Friendship shouldn't make any difference. Can you show a compilable example of what you mean? That example isn't very useful :) – jrok Aug 19 '13 at 11:59
  • Yes, it was bad, wasn't it ;-) Confirmed as a compiler bug in BCB2010 – Roddy Aug 19 '13 at 13:58
  • @jrok, Could you give me a rationale of making such a needless-seeming rule? – xmllmx Sep 25 '13 at 03:37
  • 1
    @xmllmx: The omission of `&` is only allowed for compatibility with C. Since C did not have member functions, C++ can be strict when it comes to pointers to member functions. – MSalters Dec 10 '13 at 15:43