Why there is no implicit conversions to pointers of member functions

When used std::bind, I found & is necessary to get the address of member functions, while isn’t necessary for regular functions, for example:

class Obj{
public:
    void member_func(int val);
};

void func(int val);

int main(){
    Obj obj;
    auto mf1 = std::bind(&Obj::member_func, &obj, 1); // ok
    auto mf2 = std::bind(Obj::member_func, &obj, 1); // error
    
    auto f1 = std::bind(&func, 1); // ok
    auto f2 = std::bind(func, 1); // ok
}

And after STFW(Search The Friendly Web 🙂 ), I found this in cpp reference

An lvalue of function type T can be implicitly converted to a prvalue pointer to that function. This does not apply to non-static member functions because lvalues that refer to non-static member functions do not exist.

Which explains why & is necessary for member functions, but I still don’t fully understand why there is no implicit conversions to pointers of member functions?

In particular, what does "lvalues that refer to non-static member functions do not exist" mean?

And "lvalue of function type" also confuses me, does that mean the function designator func is an lvalue?

First time to ask question, hope I clearly explained my question.

1

I still don’t fully understand why there is no implicit conversions to pointers of member functions.

It’s primarily to stop you making mistakes like this:

if (MyClass::MyFunc) ...                    // oops! would always be true, if legal

when you meant to type:

if (MyClass::MyFunc ()) ...
//                  ^^

And that’s a Good Thing ™.

But that begs the question, why don’t things work the same way for regular functions? After all, it’s perfectly legal (but, as in the example above, totally meaningless) to write:

if (MyNonMemberFunc) ...                    // oops! always true, rats!

And this is a throwback to C, where function names decay to pointers when you leave the brackets off. That has value, but overall I think most people would prefer correctness to having to add a leading & when that is the intention.

So it’s a bit unfortunate that we have different rules for member and non-member functions, but the standards committee obviously wanted to fix this particular booby trap for member functions but couldn’t for non-member functions, in order to maintain backwards compatibility. And to be fair, most modern compilers do warn you if you write if (MyNonMemberFunc) so we’re not in such a bad place with this, all things considered.

7

why there is no implicit conversions to pointers of member functions?

Pointer to non-static member functions are not actually pointers. This can be seen from the following statement in the standard:

From dcl.mptr#3’s note:

[ Note: See also [expr.unary] and [expr.mptr.oper]. The type “pointer to member” is distinct from the type “pointer”, that is, a pointer to member is declared only by the pointer to member declarator syntax, and never by the pointer declarator syntax. There is no “reference-to-member” type in C++.  — end note ]

And from basic.compound#3:

…Except for pointers to static members, text referring to “pointers” does not apply to pointers to members

(emphasis mine)

The combination of the above two quoted statements explain why we need to use & for pointer to member function as explained below.

Note the highlight part(“distinct type”) in the above quoted statement. Since a pointer to member is a distinct type and text referring to pointers does not apply to pointers to members this means that they should be treated differently. Which is why we need to explicitly add the & for them to emphasize on the fact that they are distinct from the type pointer.


Now coming to your second question:

what does “lvalues that refer to non-static member functions do not exist” mean?

The standard specifically says(in the same note quoted above) that:

There is no “reference-to-member” type in C++.

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật