Is it possible that the “use” of a keyword inherits fewer functions?

In Derivedthere template foo(T). There Baseare 2 overloads foo().

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()called with an object Derived; I want to use only Base::foo(int)if applicable, otherwise it should call Derived::foo(T).

Derived obj;
obj.foo(4);  // calls B::foo(int)
obj.foo(4.5); // calls B::foo(double) <-- can we call Derived::foo(T) ?

In short, I need an effect:

using Base::foo(int);

Is it possible? The above is an example.

+5
source share
2 answers

usingtransfers all overloads to scope. Just hide it in a derived class, this is a bit more to write, but it does the job:

struct Derived : Base
{
  template<typename T> void foo (T x) {}
  void foo(int x){
    Base::foo(x);
  }
};
+6
source

, , - ints:

template<> void Derived::foo<int> (int x) { Base::foo(x) };

using, , , .

, , , .

+1

All Articles