jeudi 31 mai 2018

Can variadic functors in C++ support named fields?

I'm implementing a variadic functor generic called "Signal" which manages an internal queue of functors of matching function signature types. On calling Signal::operator() every functor in the queue is executed with the same input args. The idea is that it is a pure C++11 object type which recreates some of the same design behaviors as Qt's Signal/Slot construct, but statically compiled with minimal dependencies. I have everything working, but I'd like to improve the readability of this generic.

Syntax I have:

Signal<int, double> mySignal;
signal.add(BLOCK, [](int EmployeeID, double FavoriteNumber) {
    std::cout << EmployeeNames[EmployeeID]
              << " has favorite number of "
              << FavoriteNumber << std::endl;
});
signal.add(BLOCK, [](int EmployeeID, double FavoriteNumber) {
    if (EmployeeID > FavoriteNumber) {
        std::cout << EmployeeNames[EmployeeID]
                  << " has ID bigger than favorite number.\n";
    }
});
mySignal(5, 3.1415); //execute both functors with args = (5, 3.1415)

What I would like to have:

Signal<int EmployeeID, double FavoriteNumber> mySignal;
signal.add(BLOCK, [](int EmployeeID, double FavoriteNumber) {
    std::cout << EmployeeNames[EmployeeID]
              << " has favorite number of "
              << FavoriteNumber << std::endl;
});
signal.add(BLOCK, [](int EmployeeID, double FavoriteNumber) {
    if (EmployeeID > FavoriteNumber) {
        std::cout << EmployeeNames[EmployeeID]
                  << " has ID bigger than favorite number.\n";
    }
});
mySignal(5, 3.1415); //execute both functors with args = (5, 3.1415)

The only difference is that I would like the declaration of the templated signal type to specify a name for the parameter for readability sake. Ideally I would like these names to be mandatory and fail compilation if unspecified.

Is there a way to achieve this?

Aucun commentaire:

Enregistrer un commentaire