How to enforce template parameter class to derive from super with an anonymous template parameter

前端 未结 2 540
离开以前
离开以前 2021-01-27 06:27

I have a couple of template classes

template < class Cost >
class Transition {
  public:
    virtual Cost getCost() = 0;
};

template < class Transition         


        
2条回答
  •  生来不讨喜
    2021-01-27 07:16

    One way is to use the return type of getCost() (but it may give you an uglier error messgae if TransactionCl() doesn't have such a public member function).

    std::is_base_of< Transition< decltype(TransitionCl().getCost()) >, TransitionCl >::value,
    

    Another option is adding a typedef to the base class:

    template < class Cost >
    class Transition {
      public:
        typedef Cost Cost_Type;    // <-------
        virtual Cost getCost() = 0;
    };
    

    Then you can remove State's typename Cost parameter and use the typedef instead in your static assert...

    std::is_base_of< Transition< typename TransitionCl::Cost_Type >, TransitionCl >::value,
    

提交回复
热议问题