[英]Workaround to partially specialize a single member of a class template?
我有一個類有兩個模板參數,doLeft/doRight 成員函數只依賴一個模板參數(即Left/Right),而doBoth 依賴兩個參數。 似乎 C++ 不支持成員的部分專業化。
近似它的最佳方法是什么?
template<typename Left, typename Right>
class RightandLeft {
void do() {
doLeft();
doRight();
doBoth();
}
void doLeft();
void doRight();
void doBoth();
}
// when left type is int64, do something based on int64,
// right type can be any type. How should I achieve it?
template<typename right>
void RightandLeft<int64_t, right>::doLeft() {
}
// when right type is string, do something based on string,
// left type can be any type. How should I achieve it?
template<typename left>
void RightandLeft<left, string>::doRight() {
}
template<typename left, typename right>
void RightandLeft<left, right>::doBoth(){};
重組,然后委托。
template<typename A>
struct Left {
void doImpl() {}
};
template<>
inline void Left<int64_t>::doImpl() {
// Do something else
}
template<typename B>
struct Right {
void doImpl() {}
};
template<>
inline void Right<string>::doImpl() {
// Do something else
}
template<typename L, typename R>
class RightandLeft : Left<L>, Right<R> {
void doImpl() {
Left<L>::doImpl();
Right<R>::doImpl();
doBoth();
}
void doBoth();
};
您不能部分專門化一個成員,因為這意味着您部分專門化了該類(您沒有)。 但是你可以完全特化一個成員,編譯器可能會假設它是主模板的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.