Is it possible for "using" keyword to inherit fewer functions?
In Derived
there is a template foo(T)
. There are 2 overloads of foo()
in Base
.
struct Base
{
void foo (int x) {}
void foo (double x) {}
};
struct Derived : Base
{
template<typename T> void foo (T x) {}
using Base::foo;
};
Now, when foo()
is called with Derived
object; I want to use only Base::foo(int)
if applicable, otherwise it should invoke Derived::foo(T)
.
Derived obj;
obj.foo(4); // calls B::foo(int)
obj.foo(4.5); // calls B::foo(double) <-- can we call Derived::fo开发者_运维技巧o(T) ?
In short, I want an effect of:
using Base::foo(int);
Is it possible ? Above is just an example.
using
bringes all overloads into the scope. Just hide it in the derived class, it's a bit more to write but does the job:
struct Derived : Base
{
template<typename T> void foo (T x) {}
void foo(int x){
Base::foo(x);
}
};
Quite a neat way to do this if you want to keep with the template approach is to specialise your template for ints:
template<> void Derived::foo<int> (int x) { Base::foo(x) };
Everything will be brought in with using
so I would avoid this in your case, but you can create as many specialisations as you need.
The good thing is that they don't litter your class declaration, but this can also bring maintenance problems if your specialisation is not obvious.
精彩评论