Can a class member function template be virtual?

前端 未结 13 1225
旧时难觅i
旧时难觅i 2020-11-22 03:29

I have heard that C++ class member function templates can\'t be virtual. Is this true?

If they can be virtual, what is an example of a scenario in which one would

13条回答
  •  暖寄归人
    2020-11-22 04:07

    To answer the second part of the question:

    If they can be virtual, what is an example of a scenario in which one would use such a function?

    This is not an unreasonable thing to want to do. For instance, Java (where every method is virtual) has no problems with generic methods.

    One example in C++ of wanting a virtual function template is a member function that accepts a generic iterator. Or a member function that accepts a generic function object.

    The solution to this problem is to use type erasure with boost::any_range and boost::function, which will allow you to accept a generic iterator or functor without the need to make your function a template.

提交回复
热议问题