different member function signatures based on class template arguments
Hello, I have a long chain of calls of functions where a template argument is passed from 1 function to the next. Different template arguments required slightly different arguments in the following signature: template< typename Interp> struct InterExtrap { static void Process( xbegin, xend, x, AdditionalArguments<Interp> ); // this calls static function inside Interp }; AdditionalArguments<Interp> is: . empty for a couple of Interp . size_t for a couple of others . double d0, double d1 for a couple of others. If all cases had exactly 1 argument which varied, I would have done above: typename AdditionalArguments<Interp>::type I could do struct S { double d1; double d2; }; I have in mind optimization issues as well as many of these intermediate functions are inlined. So I am concerned about process(xbegin, xend, x, const S& s) // then access s.d1 and s.d2 as I measured it is slower than process(xbegin, xend, x, double d1, double d2) Initial function (specifies Interp and therefore knows whether to pass empty, or size_t, or double double) calls a number of intermediate functions that are not interested in this detail, but still have the Interp template argument, and finally call static functions inside Interp which then know what they require as arguments. Is it possible to write the intermediate functions (member of template classes) in a generic way? Would SFINAE or the sizeof trick or any MPL Fusion facility allow this? Regards,
Hicham Mouline a écrit :
So I am concerned about process(xbegin, xend, x, const S& s) // then access s.d1 and s.d2 as I measured it is slower than process(xbegin, xend, x, double d1, double d2)
How did you performed your benchmark ? Most modern compiler should produce similar code for those two interface. Are you sure you pass your structure by reference ? How large is your sample measures ? -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Joel Falcou Sent: 08 April 2009 12:57 To: boost-users@lists.boost.org Subject: Re: [Boost-users] different member function signatures based on class template arguments
Hicham Mouline a écrit :
So I am concerned about process(xbegin, xend, x, const S& s) // then access s.d1 and s.d2 as I measured it is slower than process(xbegin, xend, x, double d1, double d2)
How did you performed your benchmark ? Most modern compiler should produce similar code for those two interface. Are you sure you pass your structure by reference ? How large is your sample measures ? I used g++4.1 -O3 and to be honest, the test was a few months back, I just remembered it. I'll try again now...
The question remains: isn't there a way to have more than 1 argument? Also, how do I treat the case of no extra argument at all? Regards,
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Joel Falcou Sent: 08 April 2009 12:57 To: boost-users@lists.boost.org Subject: Re: [Boost-users] different member function signatures based on class template arguments
Hicham Mouline a écrit :
So I am concerned about process(xbegin, xend, x, const S& s) // then access s.d1 and s.d2 as I measured it is slower than process(xbegin, xend, x, double d1, double d2)
How did you performed your benchmark ? Most modern compiler should produce similar code for those two interface. Are you sure you pass your structure by reference ? How large is your sample measures ?
I tried something along these lines: http://codepad.org/ieS3W2aO but it failed to compile... Shouldn't the enable_if metafct remove the invalid functions from the overload set? Regards,
Hicham Mouline a écrit :
I tried something along these lines:
but it failed to compile...
Shouldn't the enable_if metafct remove the invalid functions from the overload set?
Regards,
It probably failed because you use typename inside the enable_if condition. Make a meta-function that do your test and invoke it inside enable_if -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Joel Falcou Sent: 08 April 2009 16:24 To: boost-users@lists.boost.org Subject: Re: [Boost-users] different member function signatures based on class template arguments
Hicham Mouline a écrit :
I tried something along these lines:
but it failed to compile...
Shouldn't the enable_if metafct remove the invalid functions from the overload set?
Regards,
It probably failed because you use typename inside the enable_if condition. Make a meta-function that do your test and invoke it inside enable_if
Thanks. I moved the 1st typename out of enable_if in a separate metatfct (kind of binder1st for mpl) It still fails the compile error is here: http://codepad.org/gR04D5T5 regards,
Hicham Mouline a écrit :
Thanks. I moved the 1st typename out of enable_if in a separate metatfct (kind of binder1st for mpl)
It still fails the compile error is here:
regards,
You may want to use enable_if on the return type instead of the argument type. -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Joel Falcou Sent: 08 April 2009 19:25 To: boost-users@lists.boost.org Subject: Re: [Boost-users] different member function signatures based on class template arguments
Thanks. I moved the 1st typename out of enable_if in a separate
Hicham Mouline a écrit : metatfct
(kind of binder1st for mpl)
It still fails the compile error is here:
regards,
You may want to use enable_if on the return type instead of the argument type.
Unfortunately, it doesn't seem to work either because of the same error. And I tried g++ and MSVC. Regards,
Here is an alternative working function that doesn't use enable_if : http://codepad.org/KZ4OTKxY
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Joel Falcou Sent: 08 April 2009 16:24 To: boost-users@lists.boost.org Subject: Re: [Boost-users] different member function signatures based on class template arguments
Hicham Mouline a écrit :
I tried something along these lines:
but it failed to compile...
Shouldn't the enable_if metafct remove the invalid functions from the overload set?
Regards,
It probably failed because you use typename inside the enable_if condition. Make a meta-function that do your test and invoke it inside enable_if Here is an alternative working function that doesn't use enable_if : http://codepad.org/KZ4OTKxY
Great, this works very well.... Thank you, I can't help thinking this is a king of common pattern perhaps used inside boost itself many times, and that there is some helper "something" for this already? Because, basically I need 1 S_impl for each extra additional argument. But I suppose I can generate them with BOOST_PP Rds,
Hicham Mouline a écrit :
Because, basically I need 1 S_impl for each extra additional argument. But I suppose I can generate them with BOOST_PP
Yes it should :) ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
Hello, I have the following template template<typename C> class X { public: template
void Get (int, typename boost::call_traits<Interp>::const_reference= Interp(), typename boost::call_traits<Extrap>::const_reference= Extrap() ) const; }; template<typename C> template inline void X<C>::Get(int, typename boost::call_traits<Interp>::const_reference i, typename boost::call_traits<Extrap>::const_reference e ) { // when I get here, i.mOrder is not 2 (0) e.mOrder is not 2 (23142343) } class P { public: static const size_t defaultInterpolationOrder = 2; // quadratic polynomial
Polynomial1D(size_t order =defaultInterpolationOrder) : mOrder(order) {} Private: size_t mOrder; }; 1 example of call is x.template GetSpot
( 5 ); when I debug this P's ctor is never called and mOrder is never 2 Is my GetSpot method valid, the construction of the temporary should be bound to the const ref const P& and the temporary should have finished being constructed before we go inside the Get() method. Is there an issue with the definition of Get being out of classe? Am I calling call_traits<P>::const_reference in a wrong way? VS2005SP1 Regards,
I've just corrected call_traits<>::const_reference to const_reference<>::param_type because I am dealing with function parameters here, and I still have the same problem. Rds,
It was just P ... that was P's ctor... It's Get, not GetSpot x.template Get
( 5 ); I've added to default argument to the out-of-class definition as well and then it works... does it make any sense? H
Here is the unedited code: http://codepad.org/CDOFhSqZ As it is shown here, in debug VS2005SP1, when I get inside the out-of-class def of GetSpot i and e both show garbage. When I added = Interp() and = Extrap() to the out-of-class def, debug works fine. I didn't try in Release yet, Rds,
participants (2)
-
Hicham Mouline
-
Joel Falcou