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


Groups > comp.lang.c++ > #12407

Re: Template argument deduction and function pointers

From "Paul" <pchrist<nospam>or@yahoo.co.uk>
Newsgroups comp.lang.c++
Subject Re: Template argument deduction and function pointers
Date 2011-12-12 13:42 +0000
Organization Netfront http://www.netfront.net/
Message-ID <jc50ce$6ck$1@adenine.netfront.net> (permalink)
References <1246304802345211522.169679troplin-bluewin.ch@news.aioe.org>

Show all headers | View raw


"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 ---

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web