I have a couple of template classes
template < class Cost >
class Transition {
public:
virtual Cost getCost() = 0;
};
template < class Transition
From what I understood from your question, here is a quick solution that I have:
template < class Cost >
class Transition {
public:
virtual Cost getCost() = 0;
};
template
class Der : public Transition
{
public:
T getCost() override {
}
};
template < class Transition >
class State;
template class TransitionCl, typename Cost>
class State > {
public:
State(){
static_assert(
std::is_base_of< Transition< Cost >, TransitionCl >::value,
"TransitionCl class in State must be derived from Transition< Cost >"
);
}
};
int main()
{
Der d;
State s;
return 0;
}
In the above example, you dont have to pass the 'Cost' type while creating State object.
===== UPDATE ======
template
class Transition
{
public:
virtual Cost getCost() = 0;
virtual ~Transition() {}
};
class TD: public Transition
{
public:
int getCost() override {
std::cout << "getCost override" << std::endl;
return 42;
}
};
namespace detail {
template
struct is_base_of_cust {
// This is a bit hacky as it is based upon the internal functions
// (though public) of the Transition class.
using CostType = decltype(std::declval().getCost());
static const bool value = std::is_base_of, T>::value;
};
};
template
class State
{
protected:
State() {
static_assert(
detail::is_base_of_cust::value,
"TransitionCl class in State must be derived from Transition"
);
}
public:
virtual void apply(const TransitionCl&) = 0;
virtual ~State() {}
};
class StateImpl: public State
{
public:
void apply(const TD&) override {
std::cout << "StateImpl::apply" << std::endl;
}
};
int main() {
StateImpl impl;
return 0;
}
- 热议问题