Template argument deduction and function pointers

T

Tobias Müller

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
 
8

88888 Dihedral

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.
 
T

Tobias Müller

88888 Dihedral said:
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
 
8

88888 Dihedral

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.
 
H

Heinz Ozwirk

"88888 Dihedral" schrieb im Newsbeitrag

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
 
T

Tobias Müller

Leigh Johnston said:
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
 
T

Tobias Müller

88888 Dihedral said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top