Ashot
Ashot

Reputation: 10959

Printing the address of member function

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?

Upvotes: 0

Views: 114

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

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.

Upvotes: 2

Related Questions