0
struct Widget {
    void test() {}
};

int func() {}

int main() {
    std::cout << &Widget::test << std::endl;
    std::cout << Widget::test << std::endl;
    std::cout << func << std::endl;
    std::cout << &func << std::endl;
}

In this code only the second line of main function doesn't compile. The others print 1. Why does it print 1. Shouldn't print the address of function? And why second doesn't compile but first does?

Ashot
  • 10,807
  • 14
  • 66
  • 117
  • 1
    Try this http://stackoverflow.com/questions/2064692/how-to-print-function-pointers-with-cout – Jeff Aug 04 '13 at 14:18

1 Answers1

2

Why does it print 1. Shouldn't print the address of function?

No. std::cout can print a void*, but there's no implicit conversion from function pointer types to void* (for neither regular function pointers nor pointer-to-member types). There's a conversion from function pointer types to bool though. That's what we end up with.

And why second doesn't compile but first does?

Because the standard requires you to use & to get the address of a member function.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243