|
Boost : |
Subject: Re: [boost] determine first arg of callable
From: TONGARI (tongari95_at_[hidden])
Date: 2013-05-15 12:53:30
2013/5/16 Oliver Kowalke <oliver.kowalke_at_[hidden]>
> 2013/5/15 Peter Dimov <lists_at_[hidden]>
>
> > This still doesn't quite explain it. Let's say that my fn takes a
> > std::string. How does D know what string to pass? An empty string? "foo"?
> > "hello world"? Where does the argument value come from?
> >
>
> Fn does takes a Q< std::string > not a plain std::string (I've omitted this
> detail in my first example). Q<> might be default constructable.
>
>
> > template< typename Arg >
> > struct Q {...};
> >
>
> does something irrelevant
>
>
> > struct B
> > {
> > virtual void run() = 0;
> > };
> >
>
> interface
>
>
> > template< typename Fn, typename Arg >
> > struct D : public B
> > {
> > Fn fn;
> >
> > D( Fn fn_) : fn( fn_) {}
> >
> > void run() {
> > Q< Arg > q;
> > fn( q)
> > } // argument construction omitted
> > };
> >
>
> concrete implementation - calls default ctor of Q< Arg > in D::run() and
> passes instance of Q< Arg > to fn
>
>
> > struct X
> > {
> > B * b;
> >
> > template< typename Fn >
> > X( Fn fn) : b( 0)
> > {
> > typedef typename Magic< Fn >::arg1_type arg_type;
> > b = new D< Fn, arg_type >( fn);
> > }
> >
> > void run() { b->run(); }
> > };
> >
>
> bundles the stuff
>
> void g( Q< std::string > &) {...}
> X x( g);
> x.run(); // call g() with an instance of Q< std::string > as arg
>
> struct W
> {
> void h( Q< int > &) {...}
> };
> W w;
> X x( bind( & W::h, w, _1) );
> x.run(); // call W::h() with an instance of Q< int > as arg
>
I can hardly imagine the real use case.
Will the following acceptable?
X x(feed<Q<int> >(fn)); // call fn() with an instance of Q<int> as arg
where feed<Arg>(F) returns a wrapper that explicitly describes the arg.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk