Is it possible to check if a member function is defined for a class even if the member is inherited from an unknown base class

前端 未结 3 639
小鲜肉
小鲜肉 2021-01-18 11:11

I found similar questions and answers like this one. However, as I tried out, this SFINAE tests only succeeded if the tested member is directly defined in the class being te

3条回答
  •  梦毁少年i
    2021-01-18 12:12

    Unfortunately it wouldn't be possible at least in C++03 and I doubt in C++11 also.

    Few important points:

    1. The proposed SFINAE works only if the method is public
    2. Even if the SFINAE would have worked for base methods, the point (1) applies; because for private and protected inheritance the SFINAE may end up useless
    3. Assuming you may want to deal only with public method/inheritance, the code HasFoo::test<> can be enhanced for taking multiple parameters where a base class also can be passed; std::is_base_of<> can be used for further validation of the base/derived relationship; then apply the same logic for base class also

提交回复
热议问题