C++ Logo

std-proposals

Advanced search

Re: FDL proposal: Function Dependent Lookup

From: Gašper Ažman <gasper.azman_at_[hidden]>
Date: Thu, 24 Dec 2020 16:02:39 +0000
Given that C++ has overload sets and those can come from several places I
think this has no chance in hell of being able to be rigorously specified
:).

It's been suggested, of course.

Named parameters are probably better implemented using named constructor
parameters (khem, named initializers).

On Thu, Dec 24, 2020 at 2:10 PM connor horman via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I can see this being *very *annoying to implement.
>
> On Thu, Dec 24, 2020 at 00:48 Paco Arjonilla via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> 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!
>>
>> Cheers
>> Paco
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-12-24 10:02:53