I don't really know what this would be called if it exists, and thus I also don't know if it exists. If it does exist I don't think any compilers support it (at least none I tested).
What we can already do:1 2 3 4 5 6 7
|
void f(int);
void f(std::string);
//etc...
//elsewhere...
void (&intfunc)(int) = f;
void (&stringfunc)(std::string) = f;
|
Demo:
http://ideone.com/R3ou4C
Relevance: This shows that C++ already has a mechanic for selecting specific overloads of overloaded functions without context of a particular call.
What we currently have: (applies to both member functions and constructors)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
struct Base
{
void f(int);
void f(std::string);
//etc...
};
struct Derived : Base
{
//option 1: bring all overloads into scope, even ones we don't want
using Base::f; //`using Base::Base` for ctor
//option 2: define pass-through functions for each overload we want:
void f(int _1) { return Base::f(_1); }
void f(std::string _1){ return Base::f(_1); }
};
|
Problems with Option 1: All overloads are brought into scope, even ones we do not want. There is no way then to remove from scope the ones which should not be in scope.
Problems with Option 2: The signatures of the overloads may change in a way which allows implicit conversions in the pass-through functions, meaning that incorrect functions may be called and there will be no compiler diagnostic.
PROPOSAL: Allow new syntax for choosing which overloads to bring into scope:
1 2 3 4 5
|
struct Derived : Base
{
void f(int) = Base::f;
void f(std::string) = Base::f;
};
|
Problems solved: only the overloads we want are brought into scope, and if an overload signature changes a compiler diagnostic is generated.
So, what do you think? Does it already exist? Is it already planned for a future version of C++?