I'm a mathematician used to doing "old style" C++ programming for a long time now. I feel that some new syntactic constructions offerred by C++11 could help me achieve some better code regarding my professionnal projects. Yet as I'm no CS professionnal I must confess that I lack the knowledge to understand some examples I encounter in my self-learning process, altough I've been pretty lucky/succesful so far.
My impression is that variadic templates can be used to implement type-safe functions composition, as in this question. My concern is slightly more general since I'd like to compose functions with heterogeneous (but compatible) argument/return types. I've googled a lot and found another reference, but it seems utter "black magic" to me ;) and I won't pretend I can adapt the code in my context, although I feel I should find what I need in there.
I think the (most incomplete) code below is relatively self-explanatory as to what I'd like to achieve. In particular I believe the proper implementation will throw a compile-time error when one's trying to compose incompatible functions (Arrow here), and will need a piece of recursive template code.
template <typename Source , typename Target> class Arrow
{
Target eval (const Source &);
};
template <typename ...Arrows> class Compositor
{
template <typename ...Arrows>
Compositor (Arrows... arrows)
{
// do/call what needs be here
};
auto arrow(); // gives a function performing the functionnal composition of arrows
};
// define some classes A, B and C
int main(int argc, char **argv)
{
Arrow < A , B > arrow1;
Arrow < B , C > arrow2;
Compositor< Arrow < A , B > , Arrow < B , C > > compositor(arrow1 , arrow2);
Arrow < A , C > expected_result = compositor.arrow();
}
Ideally I'd like
Compositor
to directly subclass
Arrow < source_of_first_arrow , target_of_last_arrow>
and the method
arrow()
be replaced by the corresponding
eval()
but I felt the above code was more explanatory.
Any help will be greatly appreciated, even if it consists in a rough rebuke with a pointer to an existing (relatively basic) piece of example which will surely have escaped my search. Thanks!