If ampersands aren't needed for function pointers, why does boost::bind require one?
Asked Answered
T

1

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?

Tojo answered 19/8, 2013 at 11:1 Comment(2)
Looks like a compiler bug, friendship shouldn't make any difference, the & is required for forming a pointer to member.Buckner
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 &.Ferryman
F
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.

Ferryman answered 19/8, 2013 at 11:2 Comment(4)
@Tojo Friendship shouldn't make any difference. Can you show a compilable example of what you mean? That example isn't very useful :)Ferryman
Yes, it was bad, wasn't it ;-) Confirmed as a compiler bug in BCB2010Tojo
@jrok, Could you give me a rationale of making such a needless-seeming rule?Vallo
@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.Bruin

© 2022 - 2024 — McMap. All rights reserved.