我有幾個模板類的如何執行模板參數類從超派生使用匿名模板參數
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;
};
,我寧可不要通過Cost
爲State
,因爲State
是完全獨立的Cost
,但我想確保TransitionCl
實現接口Transition
。
有沒有辦法在第二個模板中使Cost
匿名,以便在聲明新的State
類時不必傳遞它?
僅供參考,我使用g++ -std=c++14 [source file]
編輯:我張貼的問題的改寫(希望更清晰)版本,並獲得最佳答案here
這不允許我做'class StateImpl:public State < OpImpl > {...};'我試圖說清楚當我寫'virtual void apply(const TransitionCl&)= 0;'時,該狀態是一個抽象類。有沒有乾淨的方法? –
@KyleMcCormick是的,我錯過了。我已經更新了我的答案。最終歸結爲託尼D在回答中所說的話。 – Arunmu