Template argument deduction and function pointers

Discussion in 'C++' started by Tobias Müller, Dec 10, 2011.

  1. 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
     
    Tobias Müller, Dec 10, 2011
    #1
    1. Advertising

  2. 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.
     
    88888 Dihedral, Dec 10, 2011
    #2
    1. Advertising

  3. 88888 Dihedral <> 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
     
    Tobias Müller, Dec 10, 2011
    #3
  4. On Saturday, December 10, 2011 10:40:59 PM UTC+8, Tobias Müller wrote:
    > 88888 Dihedral <> 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++!
     
    88888 Dihedral, Dec 10, 2011
    #4
  5. 88888 Dihedral <> 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
     
    Tobias Müller, Dec 10, 2011
    #5
  6. On Sunday, December 11, 2011 2:53:35 AM UTC+8, Tobias Müller wrote:
    > 88888 Dihedral <> 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.
     
    88888 Dihedral, Dec 10, 2011
    #6
  7. Tobias Müller

    Heinz Ozwirk Guest

    "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
     
    Heinz Ozwirk, Dec 11, 2011
    #7
  8. Leigh Johnston <> 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
     
    Tobias Müller, Dec 13, 2011
    #8
  9. 88888 Dihedral <> 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
     
    Tobias Müller, Dec 13, 2011
    #9
  10. Tobias Müller

    Joe keane Guest

    It's actually a rugby position.

    Maybe he should use 'flange'?

    TO
     
    Joe keane, Dec 14, 2011
    #10
  11. Tobias Müller

    Ian Collins Guest

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


    What is?

    Context!

    --
    Ian Collins
     
    Ian Collins, Dec 14, 2011
    #11
  12. Tobias Müller

    gwowen Guest

    On Dec 14, 9:04 pm, Ian Collins <> 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...
     
    gwowen, Dec 15, 2011
    #12
  13. Tobias Müller

    Nobody Guest

    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).
     
    Nobody, Dec 15, 2011
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    4,352
  2. Peng Yu

    Template argument deduction

    Peng Yu, Apr 16, 2005, in forum: C++
    Replies:
    1
    Views:
    509
    Mike Wahler
    Apr 16, 2005
  3. Bart Samwel
    Replies:
    14
    Views:
    813
    Bart Samwel
    Apr 22, 2005
  4. George
    Replies:
    4
    Views:
    413
    George
    Jan 13, 2006
  5. vectorizor
    Replies:
    8
    Views:
    419
    Victor Bazarov
    Jun 21, 2007
Loading...

Share This Page