0

In the following example sin.connect accepts a const reference. Why both sig.connect(printHello); and sig.connect(&printHello); compile without problems and produce the same result? What is the difference between passing a val and ref to a function that accept a reference?

#include <boost/signals2.hpp>
#include <iostream>

void printHello()
{
  std::cout << "Hello World" << std::endl;
}

int main(){
    boost::signals2::signal<void ()> sig;
    sig.connect(printHello); // <- slot reference
    sig.connect(&printHello); // <- slot
    // sig.connect(const slot_type &slot) <- signature
    sig();
}
georgeok
  • 5,321
  • 2
  • 39
  • 61
  • 1
    Functions are automatically converted to pointers to themselves most of the time, including here. – HolyBlackCat Aug 29 '20 at 14:55
  • So in this example it’s the same right? Is there any case that we need to explicitly need to pass it with & ?? – georgeok Aug 29 '20 at 17:43
  • 1
    Here yes, they're same. No, you don't need `&` 99% of the time. It's [possible to write a function](https://gcc.godbolt.org/z/461rsh) that will only accept other functions with `&`, but there's no reason to do that. Note that the situation is completely different with non-static member functions. – HolyBlackCat Aug 29 '20 at 17:51

0 Answers0