I have a couple of template classes
template < class Cost >
class Transition {
public:
virtual Cost getCost() = 0;
};
template < class TransitionCl, class Cost >
class State {
protected:
State(){
static_assert(
std::is_base_of< Transition< Cost >, TransitionCl >::value,
"TransitionCl class in State must be derived from Transition< Cost >"
);
}
public:
virtual void apply( const TransitionCl& ) = 0;
};
and I would rather not have to pass Cost
into State
, because State
is completely independent on Cost
, but I want to ensure that TransitionCl
implements the Transition
interface.
Is there a way to make Cost
anonymous in the second template so as not to have to pass it when declaring a new State
class?
For reference, I'm using g++ -std=c++14 [source file]
EDIT: I posted a rephrased (and hopefully clearer) version of the question and received the best answer here
From what I understood from your question, here is a quick solution that I have:
In the above example, you dont have to pass the 'Cost' type while creating State object.
===== UPDATE ======
One way is to use the return type of
getCost()
(but it may give you an uglier error messgae ifTransactionCl()
doesn't have such a public member function).Another option is adding a
typedef
to the base class:Then you can remove
State
'stypename Cost
parameter and use thetypedef
instead in your static assert...