Restrict C++ Template Parameter to Subclass

前端 未结 7 1600
醉话见心
醉话见心 2020-11-28 22:04

How can I force a template parameter T to be a subclass of a specific class Baseclass? Something like this:

template 

        
7条回答
  •  清酒与你
    2020-11-28 22:28

    You don't need concepts, but you can use SFINAE:

    template 
    boost::enable_if< boost::is_base_of::value >::type function() {
       // This function will only be considered by the compiler if
       // T actualy derived from Base
    }
    

    Note that this will instantiate the function only when the condition is met, but it will not provide a sensible error if the condition is not met.

提交回复
热议问题