如果我的基類具有函數func(int)
,並且我的派生類具有函數func(double)
,則導出的func(double)
將隱藏base::func(int)
。我可以用using
使底座的版本到重載的派生的列表:我需要類似「使用Base :: *;」
struct base
{
void func(int);
};
struct derived : base
{
using base::func;
void func(double);
}
OK,太好了。但是如果我不確定base
是否有func()
?即因爲我在做模板元編程,所以我不確定base
是什麼,但是我想把它的功能提升到相同的水平 - 如果它們存在的話。即,上面的例子中改變爲:
struct base_with
{
void func(int);
};
struct base_without
{
};
template <typename Base>
struct derived : Base
{
using Base::func; // if Base has a func(), I want to bring it in
void func(double);
}
derived<base_with> testwith; // compiles
derived<base_without> testwithout; // fails :-(
我需要using_if
像boost::enable_if
。似乎不可能...
在此先感謝。
你最終想做什麼?什麼是派生的'用於? –
我正在構建可以實現某些功能的類,具體取決於它們的使用方式/位置。這基本上只是重構,在一定程度上 - 即不是編寫XWithFeatureA類,XWithFeatureB類,XWithFeatureAandB類......這會變成許多樣板類,我希望有X,其中feature_traits決定使用哪些特徵。 –
tony
對於系統中的對象,諸如'允許偵聽器','具有自定義setter'等的功能。我不認爲我可以(或者被允許)描述它,以至於你確信具有可選功能的系統實際上是有意義的。 – tony