Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12295 > unrolled thread

Template argument deduction and function pointers

Started byTobias Müller <troplin@bluewin.ch>
First post2011-12-10 12:31 +0000
Last post2011-12-13 12:18 +0000
Articles 16 — 9 participants

Back to article view | Back to comp.lang.c++


Contents

  Template argument deduction and function pointers Tobias Müller <troplin@bluewin.ch> - 2011-12-10 12:31 +0000
    Re: Template argument deduction and function pointers 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-10 05:41 -0800
      Re: Template argument deduction and function pointers Tobias Müller <troplin@bluewin.ch> - 2011-12-10 14:40 +0000
        Re: Template argument deduction and function pointers 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-10 07:22 -0800
          Re: Template argument deduction and function pointers Tobias Müller <troplin@bluewin.ch> - 2011-12-10 18:53 +0000
            Re: Template argument deduction and function pointers 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-10 11:20 -0800
              Re: Template argument deduction and function pointers "Heinz Ozwirk" <hozwirk@arcor.de> - 2011-12-11 18:59 +0100
                Re: Template argument deduction and function pointers jgk@panix.com (Joe keane) - 2011-12-14 20:56 +0000
                  Re: Template argument deduction and function pointers Ian Collins <ian-news@hotmail.com> - 2011-12-15 10:04 +1300
                    Re: Template argument deduction and function pointers gwowen <gwowen@gmail.com> - 2011-12-14 23:36 -0800
                    Re: Template argument deduction and function pointers Nobody <nobody@nowhere.com> - 2011-12-15 22:53 +0000
              Re: Template argument deduction and function pointers Tobias Müller <troplin@bluewin.ch> - 2011-12-13 07:13 +0000
    Re: Template argument deduction and function pointers "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-12 13:42 +0000
    Re: Template argument deduction and function pointers Leigh Johnston <leigh@i42.co.uk> - 2011-12-12 14:01 +0000
      Re: Template argument deduction and function pointers Tobias Müller <troplin@bluewin.ch> - 2011-12-13 07:13 +0000
        Re: Template argument deduction and function pointers Leigh Johnston <leigh@i42.co.uk> - 2011-12-13 12:18 +0000

#12295 — Template argument deduction and function pointers

FromTobias Müller <troplin@bluewin.ch>
Date2011-12-10 12:31 +0000
SubjectTemplate argument deduction and function pointers
Message-ID<1246304802345211522.169679troplin-bluewin.ch@news.aioe.org>
template <typename R, typename P1, typename P2, R (*f)(P1,P2)>
class MyClass
{
  R myMethod(P1 param1, P2 param2);
};

This works, however I must always specify all the template arguments R, P1,
and P2, even if those could always be deduced from the 4th argument f:

int fun(double a, int b);

MyClass<int, double, int, &fun> myObject;
int c = myObject.myMethod(1.0, 2);

But I want some different template such that I can write:

MyClass<&fun> myObject;

All the information about R, P1, and P2 is already implicitly there.

Tobi

[toc] | [next] | [standalone]


#12297

From88888 Dihedral <dihedral88888@googlemail.com>
Date2011-12-10 05:41 -0800
Message-ID<19632393.652.1323524517570.JavaMail.geo-discussion-forums@prol32>
In reply to#12295
On Saturday, December 10, 2011 8:31:58 PM UTC+8, Tobias Müller wrote:
> template <typename R, typename P1, typename P2, R (*f)(P1,P2)>
> class MyClass
> {
>   R myMethod(P1 param1, P2 param2);
> };
> 
> This works, however I must always specify all the template arguments R, P1,
> and P2, even if those could always be deduced from the 4th argument f:
> 
> int fun(double a, int b);
> 
> MyClass<int, double, int, &fun> myObject;
> int c = myObject.myMethod(1.0, 2);
> 
> But I want some different template such that I can write:
> 
> MyClass<&fun> myObject;
> 
> All the information about R, P1, and P2 is already implicitly there.
> 
> Tobi

There's no saving in typing. 
Just use a functor in c elegantly.

[toc] | [prev] | [next] | [standalone]


#12305

FromTobias Müller <troplin@bluewin.ch>
Date2011-12-10 14:40 +0000
Message-ID<1337659521345220519.215885troplin-bluewin.ch@news.aioe.org>
In reply to#12297
88888 Dihedral <dihedral88888@googlemail.com> wrote:
> On Saturday, December 10, 2011 8:31:58 PM UTC+8, Tobias Müller wrote:
>> template <typename R, typename P1, typename P2, R (*f)(P1,P2)>
>> class MyClass
>> {
>>   R myMethod(P1 param1, P2 param2);
>> };
>> 
>> This works, however I must always specify all the template arguments R, P1,
>> and P2, even if those could always be deduced from the 4th argument f:
>> 
>> int fun(double a, int b);
>> 
>> MyClass<int, double, int, &fun> myObject;
>> int c = myObject.myMethod(1.0, 2);
>> 
>> But I want some different template such that I can write:
>> 
>> MyClass<&fun> myObject;
>> 
>> All the information about R, P1, and P2 is already implicitly there.
>> 
>> Tobi
> 
> There's no saving in typing. 
> Just use a functor in c elegantly.

Of course not, if the function always has the same signature. But I don't
want to define a class for every function signature I use in that way,
especially if I can (potentially) solve it via a template.

Tobi

[toc] | [prev] | [next] | [standalone]


#12310

From88888 Dihedral <dihedral88888@googlemail.com>
Date2011-12-10 07:22 -0800
Message-ID<32687049.683.1323530571695.JavaMail.geo-discussion-forums@prol32>
In reply to#12305
On Saturday, December 10, 2011 10:40:59 PM UTC+8, Tobias Müller wrote:
> 88888 Dihedral <dihedr...@googlemail.com> wrote:
> > On Saturday, December 10, 2011 8:31:58 PM UTC+8, Tobias Müller wrote:
> >> template <typename R, typename P1, typename P2, R (*f)(P1,P2)>
> >> class MyClass
> >> {
> >>   R myMethod(P1 param1, P2 param2);
> >> };
> >> 
> >> This works, however I must always specify all the template arguments R, P1,
> >> and P2, even if those could always be deduced from the 4th argument f:
> >> 
> >> int fun(double a, int b);
> >> 
> >> MyClass<int, double, int, &fun> myObject;
> >> int c = myObject.myMethod(1.0, 2);
> >> 
> >> But I want some different template such that I can write:
> >> 
> >> MyClass<&fun> myObject;
> >> 
> >> All the information about R, P1, and P2 is already implicitly there.
> >> 
> >> Tobi
> > 
> > There's no saving in typing. 
> > Just use a functor in c elegantly.
> 
> Of course not, if the function always has the same signature. But I don't
> want to define a class for every function signature I use in that way,
> especially if I can (potentially) solve it via a template.
> 
> Tobi

http://www.newty.de/fpt/fpt.html

Check the true functional c /c++! 

[toc] | [prev] | [next] | [standalone]


#12316

FromTobias Müller <troplin@bluewin.ch>
Date2011-12-10 18:53 +0000
Message-ID<548924579345235416.652465troplin-bluewin.ch@news.aioe.org>
In reply to#12310
88888 Dihedral <dihedral88888@googlemail.com> wrote:
> 
> http://www.newty.de/fpt/fpt.html
> 
> Check the true functional c /c++!

Have you also read chapter 4? (about functors)

The setup that I posted is just to illustrate my point, not exactly what I
am using.
It's not restricted to functions, also methods should be possible.

Tobi

[toc] | [prev] | [next] | [standalone]


#12317

From88888 Dihedral <dihedral88888@googlemail.com>
Date2011-12-10 11:20 -0800
Message-ID<16300971.1743.1323544844136.JavaMail.geo-discussion-forums@pril5>
In reply to#12316
On Sunday, December 11, 2011 2:53:35 AM UTC+8, Tobias Müller wrote:
> 88888 Dihedral <dihedr...@googlemail.com> wrote:
> > 
> > http://www.newty.de/fpt/fpt.html
> > 
> > Check the true functional c /c++!
> 
> Have you also read chapter 4? (about functors)
> 
> The setup that I posted is just to illustrate my point, not exactly what I
> am using.
> It's not restricted to functions, also methods should be possible.
> 
> Tobi

I use a function pointer int (*hooker)() in most applications. 
It is the programmer's job to hook the correct address of a function to 
the hooker. 

Also the argument to be used must be programmed right. 
 
There are situations that the hookers are in a library that will be hooked 
by users other than the author of the library.

Then please check the functionoid that requires specific arguments in the 
run time. 

[toc] | [prev] | [next] | [standalone]


#12355

From"Heinz Ozwirk" <hozwirk@arcor.de>
Date2011-12-11 18:59 +0100
Message-ID<4ee4ef88$0$6571$9b4e6d93@newsspool3.arcor-online.net>
In reply to#12317
"88888 Dihedral"  schrieb im Newsbeitrag 
news:16300971.1743.1323544844136.JavaMail.geo-discussion-forums@pril5...

On Sunday, December 11, 2011 2:53:35 AM UTC+8, Tobias Müller wrote:
> I use a function pointer int (*hooker)() in most applications.
> It is the programmer's job to hook the correct address of a function to
> the hooker.

OT: Don't use rules of your native language to invent new words in foreign 
languages. Lookup the meaning of hooker in a dictionary (like leo.org).

Sorry for being off topic, but I simply couldn't resist.
    Heinz 

[toc] | [prev] | [next] | [standalone]


#12512

Fromjgk@panix.com (Joe keane)
Date2011-12-14 20:56 +0000
Message-ID<jcb2hd$qfg$1@reader1.panix.com>
In reply to#12355
It's actually a rugby position.

Maybe he should use 'flange'?

TO

[toc] | [prev] | [next] | [standalone]


#12514

FromIan Collins <ian-news@hotmail.com>
Date2011-12-15 10:04 +1300
Message-ID<9kshbkFhipU13@mid.individual.net>
In reply to#12512
On 12/15/11 09:56 AM, Joe keane wrote:
> It's actually a rugby position.

What is?

Context!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#12531

Fromgwowen <gwowen@gmail.com>
Date2011-12-14 23:36 -0800
Message-ID<9e0b1393-525b-4d65-8ccf-f9023fe71f21@v29g2000yqv.googlegroups.com>
In reply to#12514
On Dec 14, 9:04 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 12/15/11 09:56 AM, Joe keane wrote:
>
> > It's actually a rugby position.
>
> What is?
>
> Context!

The problem is he deleted the first 5/8 of the post...

[toc] | [prev] | [next] | [standalone]


#12556

FromNobody <nobody@nowhere.com>
Date2011-12-15 22:53 +0000
Message-ID<pan.2011.12.15.22.53.31.711000@nowhere.com>
In reply to#12514
On Thu, 15 Dec 2011 10:04:52 +1300, Ian Collins wrote:

> On 12/15/11 09:56 AM, Joe keane wrote:
>> It's actually a rugby position.
> 
> What is?

"Hooker" is the centre-forward position in rugby (union and league).

[toc] | [prev] | [next] | [standalone]


#12426

FromTobias Müller <troplin@bluewin.ch>
Date2011-12-13 07:13 +0000
Message-ID<1938084733345452372.606690troplin-bluewin.ch@news.aioe.org>
In reply to#12317
88888 Dihedral <dihedral88888@googlemail.com> wrote:

> I use a function pointer int (*hooker)() in most applications. 
> It is the programmer's job to hook the correct address of a function to 
> the hooker. 
> 
> Also the argument to be used must be programmed right. 
>  
> There are situations that the hookers are in a library that will be hooked 
> by users other than the author of the library.
> 
> Then please check the functionoid that requires specific arguments in the 
> run time.

Well I have a working and type safe solution, it is just a bit verbose. But
I certainly don't want to lose those features.

Tobi

[toc] | [prev] | [next] | [standalone]


#12407

From"Paul" <pchrist<nospam>or@yahoo.co.uk>
Date2011-12-12 13:42 +0000
Message-ID<jc50ce$6ck$1@adenine.netfront.net>
In reply to#12295
"Tobias Müller" <troplin@bluewin.ch> wrote in message 
news:1246304802345211522.169679troplin-bluewin.ch@news.aioe.org...
> template <typename R, typename P1, typename P2, R (*f)(P1,P2)>
> class MyClass
> {
>  R myMethod(P1 param1, P2 param2);
> };
>
> This works, however I must always specify all the template arguments R, 
> P1,
> and P2, even if those could always be deduced from the 4th argument f:
>
> int fun(double a, int b);
>
> MyClass<int, double, int, &fun> myObject;
> int c = myObject.myMethod(1.0, 2);
>
> But I want some different template such that I can write:
>
> MyClass<&fun> myObject;
>
> All the information about R, P1, and P2 is already implicitly there.
>
You can't do it like that , this may help though....

#include <iostream>

template< typename T=int>
class Test{
public:
 template<typename P, typename R>
 void bar( R (*fp)(P) ){
  fp(5);
 }
};

void foo1(int x){
 std::cout<< "\nIn foo1..." << std::endl;
}
double foo2(double x){
 std::cout<< "\nIn foo2..." << std::endl;
 return 3.3;
}

int main(){
 Test<> t;

 t.bar(&foo1);
 t.bar(&foo2);

}




--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#12409

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-12-12 14:01 +0000
Message-ID<3N2dnYH2AqzGlHvTnZ2dnUVZ8nednZ2d@giganews.com>
In reply to#12295
On 10/12/2011 12:31, Tobias Müller wrote:
> template<typename R, typename P1, typename P2, R (*f)(P1,P2)>
> class MyClass
> {
>    R myMethod(P1 param1, P2 param2);
> };
>
> This works, however I must always specify all the template arguments R, P1,
> and P2, even if those could always be deduced from the 4th argument f:
>
> int fun(double a, int b);
>
> MyClass<int, double, int,&fun>  myObject;
> int c = myObject.myMethod(1.0, 2);
>
> But I want some different template such that I can write:
>
> MyClass<&fun>  myObject;
>
> All the information about R, P1, and P2 is already implicitly there.

If you have a C++11 compiler you could perhaps write a make function and 
make use of the auto keyword:

auto myObject = make_MyClass(&fun);
int c = myObject.myMethod(1.0, 2);

HTH.

/Leigh

[toc] | [prev] | [next] | [standalone]


#12425

FromTobias Müller <troplin@bluewin.ch>
Date2011-12-13 07:13 +0000
Message-ID<1663712966345452790.281157troplin-bluewin.ch@news.aioe.org>
In reply to#12409
Leigh Johnston <leigh@i42.co.uk> wrote:
> If you have a C++11 compiler you could perhaps write a make function and
> make use of the auto keyword:
> 
> auto myObject = make_MyClass(&fun);
> int c = myObject.myMethod(1.0, 2);
> 
> HTH.
> 
> /Leigh

That would probably work but I cannot use c++11 features, and second, I
want to use the objects mainly as members of other classes, so auto
wouldn't work anyway in that context.

Tobi

[toc] | [prev] | [next] | [standalone]


#12439

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-12-13 12:18 +0000
Message-ID<lIadnTirj5kg33rTnZ2dnUVZ8iydnZ2d@giganews.com>
In reply to#12425
On 13/12/2011 07:13, Tobias Müller wrote:
> Leigh Johnston<leigh@i42.co.uk>  wrote:
>> If you have a C++11 compiler you could perhaps write a make function and
>> make use of the auto keyword:
>>
>> auto myObject = make_MyClass(&fun);
>> int c = myObject.myMethod(1.0, 2);
>>
>> HTH.
>>
>> /Leigh
>
> That would probably work but I cannot use c++11 features, and second, I
> want to use the objects mainly as members of other classes, so auto
> wouldn't work anyway in that context.

Have you considered std::(tr1::)bind and std::(tr1::)function or Boost.bind?

/Leigh

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web