Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68255
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: forward variable number of arguments precisely to a template method |
| Date | 2019-09-06 03:17 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86imq5wwyj.fsf@mailhub.linuxsc.com> (permalink) |
| References | <pxjbF.468705$tG2.346514@fx48.iad> |
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> writes:
> Trying to write generic code for forwarding arguments via a
> template method to methods defined in an interface precisely (in
> particular, all references should be forwarded as references and
> values as values). Using gcc 4.8.5 and -std=c++11 (cannot
> change). The best code I could come up with so far is below.
>
> Taking the below code as an example, I want generated `callMethod'
> to have exactly same last 2 parameter types as the interface
> method it calls ((I,I) for ma, (const I&, const I&) for mb and
> (const I&, I) for mc). [...]
I took a pretty long look at this. I think you're up against a
hard problem. Here's the best I came up with (please excuse
some minor changes in names, etc):
template< typename T, const int >
class Caller {
T &worker;
public:
Caller( T &w ) : worker( w ) {}
template< typename ... Stuff > void
invoke( void (T::*pmf)( Stuff ... ), Stuff ... stuff ){
(worker.*pmf)( stuff ... );
}
};
and in main(), instead of
Caller<IA,1>(a).callMethod( &IA::ma, i1, i2 );
Caller<IA,2>(a).callMethod( &IA::mb, i3, i4 );
Caller<IA,3>(a).callMethod( &IA::mc, i5, i6 );
I have
Caller<IA,1>(a).invoke< I , I >( &IA::ma, i1, i2 );
Caller<IA,2>(a).invoke< const I &, const I & >( &IA::mb, i3, i4 );
Caller<IA,3>(a).invoke< const I &, I >( &IA::mc, i5, i6 );
I looked at using forward(), but I don't think it helps you. I
should add though that I'm still pretty much of a novice with
those things.
My sense is that you will be stuck with having to give the
template argument types explicitly rather than having them be
deduced. I have a similar disclaimer about not having any
level of expertise in template type deduction.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
forward variable number of arguments precisely to a template method Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-02 21:41 -0400
Re: forward variable number of arguments precisely to a template method Öö Tiib <ootiib@hot.ee> - 2019-09-03 00:24 -0700
Re: forward variable number of arguments precisely to a template method Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-04 10:47 -0400
Re: forward variable number of arguments precisely to a template method Manfred <noname@add.invalid> - 2019-09-05 20:21 +0200
Re: forward variable number of arguments precisely to a template method Vir Campestris <vir.campestris@invalid.invalid> - 2019-09-03 21:29 +0100
Re: forward variable number of arguments precisely to a template method Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-04 11:08 -0400
Re: forward variable number of arguments precisely to a template method Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-06 03:17 -0700
Re: forward variable number of arguments precisely to a template method Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-06 23:48 -0400
Problem solved: forward variable number of arguments precisely to a template method Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-07 10:16 -0400
Re: Problem solved: forward variable number of arguments precisely to a template method Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-18 08:22 -0700
csiph-web