[英]Make template function to destinguish inheritors and others
我有一個類( base
),它有許多繼承者( derived_i
)和一些其他類型( other
)。 我想在一些類處理程序( caller
程序)中有一個模板方法,它以不同的方式處理base
繼承者,然后從其他類型處理。
這是我講述的示例代碼。
#include <iostream>
using namespace std;
template <typename T>
class base {
public:
base (T val = T())
: m_val(val)
{}
base (const base &other)
: base(other.m_val)
{}
virtual void base_specific_method()
{
cout << __func__ << " method called: " << m_val << endl;
}
void each_class_has_this() {
cout << __func__ << " this is boring..." << endl;
}
T m_val;
};
class other {
public:
void each_class_has_this() {
cout << __func__ <<" this is boring..." << endl;
}
};
class derived_i : public base <int>
{
public:
derived_i () : base <int> (10)
{}
virtual void base_specific_method()
{
cout << __func__ <<" Hey! I'm interesting derived! And 10 == " << m_val << endl;
}
};
template <typename T>
class caller {
public:
caller (T val = T())
: m_val(val)
{}
void call() {
p_call(m_val);
}
private:
template <typename T1> void p_call (T1 &val)
{
val.each_class_has_this();
}
template <typename T1> void p_call (base<T1> &val)
{
val.base_specific_method();
}
private:
T m_val;
};
int main ()
{
caller<other> c1;
caller<base<double> > c2;
caller<derived_i > c3;
c1.call();
c2.call();
c3.call();
}
它用g++ -std=c++11 test.cpp
,輸出如下:
each_class_has_this this is boring...
base_specific_method method called: 0
each_class_has_this this is boring...
雖然我在期待
each_class_has_this this is boring...
base_specific_method method called: 0
base_specific_method Hey! I'm interesting derived! And 10 == 10
有沒有辦法更改此代碼,使其適合我的要求?
這個問題似乎是另一個問題的重復,但正確的答案會導致問題,我在這里面對。
PS沒有辦法從一個類中創建base
和other
繼承者。 =(
您可以使用SFINAE獲得所需的行為:
像這樣添加一個類has_specific
:
template <typename T>
class has_specific
{
typedef char one;
typedef long two;
template <typename C> static one test( typeof(&C::base_specific_method) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
並將p_call
的定義p_call
為:
template <typename T1=T>
typename enable_if<!has_specific<T1>::value,void>::type
p_call (T1 &val)
{
val.each_class_has_this();
}
template <typename T1=T>
typename enable_if<has_specific<T1>::value,void>::type
p_call (T1 &val)
{
val.base_specific_method();
}
在這種情況下,我在返回類型中使用了SFINAE。 應該可以在模板參數列表或參數列表中使用它,但這個是我首先工作的那個。
作為一個技術細節,這個實現不依賴於你是否有一個派生自base
的類,但是如果存在一個方法base_specific_method()
,但我希望它仍然有助於你的問題。
您可以將std::enable_if
與std::is_base_of
以區分派生自base<T>
的類。
template <typename T, typename TT = void>
class caller {
public:
caller (T val = T())
: m_val(val)
{}
void call() {
p_call(m_val);
}
private:
template <typename T1> typename std::enable_if<!std::is_base_of<base<TT>, T1>::value>::type p_call (T1 &val)
{
val.each_class_has_this();
}
template <typename T1> typename std::enable_if<std::is_base_of<base<TT>, T1>::value>::type p_call(T1& val)
{
val.base_specific_method();
}
private:
T m_val;
};
請注意, base
是一個模板類,這意味着T1
可能是base<int>
或base<double>
派生類,依此類推。 我們無法計算所有可能性,因此需要將模板參數TT
指定為提示。 然后用它作為:
caller<other> c1;
caller<base<double>, double> c2;
caller<derived_i, int> c3; // derived_i derived from base<int>
c1.call();
c2.call();
c3.call();
結果:
each_class_has_this this is boring...
base_specific_method method called: 0
base_specific_method Hey! I'm interesting derived! And 10 == 10
或者你可以創建一個最基礎的類來使它變得簡單:
class abstract_base {
public:
virtual void base_specific_method() = 0;
virtual ~abstract_base() {}
};
template <typename T>
class base : public abstract_base {
...
};
...
template <typename T>
class caller {
...
template <typename T1> typename std::enable_if<!std::is_base_of<abstract_base, T1>::value>::type p_call (T1 &val)
{
val.each_class_has_this();
}
template <typename T1> typename std::enable_if<std::is_base_of<abstract_base, T1>::value>::type p_call(T1& val)
{
val.base_specific_method();
}
...
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.