【发布时间】:2011-08-07 07:20:54
【问题描述】:
如果我的基类有一个函数func(int),而我的派生类有一个函数func(double),则派生的func(double) 隐藏base::func(int)。我可以使用using 将基本版本带入派生的重载列表中:
struct base
{
void func(int);
};
struct derived : base
{
using base::func;
void func(double);
}
好的,太好了。但是如果我不确定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。好像不太可能……
提前致谢。
【问题讨论】:
-
你最终想做什么?
derived<XXX>是干什么用的? -
我正在构建可以选择实现某些功能的类,具体取决于它们的使用方式/位置。在某种程度上,这基本上只是重构 - 即不是编写类 XWithFeatureA, XWithFeatureB, XWithFeatureAandB, ... 这会变成许多样板类,我想要 X
其中 feature_traits 决定使用哪些功能。 -
系统中的对象的功能是“允许侦听器”、“具有自定义设置器”等。我认为我无法(或被允许)将其描述得足以让您确信具有可选功能的系统实际上是有意义的。
-
我很困惑,谁是托尼,谁是托尼?看起来像是一种轻度的身份障碍:)
标签: c++ metaprogramming