C++ Logo

std-proposals

Advanced search

FDL proposal: Function Dependent Lookup

From: Paco Arjonilla <pacoarjonilla_at_[hidden]>
Date: Thu, 24 Dec 2020 05:46:19 +0000 (UTC)
Hi all,

I want to propose a variation to ADL that goes in the opposite direction. Instead of function lookup dependent on arguments, it would perform argument lookup dependant on the function. I will call it Function Dependent Lookup (FDL) from here on.

For example,

```
namespace A {
    constexpr int x = 1;
    void f(int);
}
int main() {
    A::f(x);
}
```

With the current C++ standard, the above code does not compile. To fix it, we can either indicate the namespace for x
```
int main() {
    A::f(A::x);
}
```
or add a using statement.
```
int main() {
    using namespace A;
    A::f(x);
}
```

The motivation for the proposal is to enable named keyword arguments without polluting the scope. Keyword arguments are possible by overloading operator = and using several auxiliar classes. For example, consider the following model snippet

```
void foo() {
    matrixlib::matrix_t data = matrixlib::load("/Path/to/training/data");
    
    // using namespace nnlib; /* This line is necessary, but it pollutes
                                 the scope with the identifiers of all
                                 named keywords! */
    auto trained_nn = nnlib::train_deep_neural_network( data, hidden_layers=2,
        learning_rate=0.09,
        momentum=0.9,
        maximum_epochs=100000,
        acceptable_error=10e-5
        );
    ...
}
```

FDL would enable using C++ function calls with python-like syntax without polluting the scope. It would make easier for users of python and other languages to migrate to C++ and to write libraries in C++ with similar syntax. Neural Network libraries is a typical example.


Possible ambiguity resolution with ADL:
To avoid ambiguities with ADL and make FDL compatible with ADL, FDL would only be enabled when the function is called with a scope resolution, i.e. `scope::function(keyword=value);`


For a working use case, see the experimental library namiplot.

namiplot.gitlab.io

 Namiplot uses named keywords to set graphic parameters in order to plot data. Compilation tested with GCC10. Specifically, FDL would be advantageous in the working example at:

https://gitlab.com/namiplot/namiplot/-/blob/master/examples/data_types/functions.cc


Thank you for reading this far!
CheersPaco


Received on 2020-12-23 23:48:24