parameter deduction in function object

M

Marcel Sebbao

I want to use a function object for various callbacks, and also as a
normal function.

But this function is a template. It is fine, but then I need to
specify all the time the template
parameter although it is always the same as the constructor argument:


e.g:

template<typename C>
class func {
const C& refClass;
public:
func(const C& aClass) : c(refClass) {}
double operator () (const double& x) const {
return c.(x);
};
};

class myClass {
int a,b;
public:
myClass(const int A, const int B) : a(A), b(B) {}
double ez(const double& x) const {
return pow(cos(x), a) + pow(sin(x), b);
}
};

myClass mc(2,3);
func<myClass> f(mc);
for (int i=0; i<100; ++i)
std::cout << f(i*0.01) << ' ';
std::cout << std::endl;


Well is there any way to declare func such as:
func f(mc) ?
 
D

David Harmon

On 29 Mar 2005 08:39:22 -0800 in comp.lang.c++, (e-mail address removed)
(Marcel Sebbao) wrote,
I want to use a function object for various callbacks, and also as a
normal function.

But this function is a template. It is fine, but then I need to
specify all the time the template
parameter although it is always the same as the constructor argument:

Parameter deduction is done for straight function templates, but
not for class templates. That is the reason why, for example,
std::bind1st() exists as a helper to construct a std::binder1st
instance. Perhaps you want to follow that approach.
 
V

Victor Bazarov

Marcel said:
I want to use a function object for various callbacks, and also as a
normal function.

But this function is a template. It is fine, but then I need to
specify all the time the template
parameter although it is always the same as the constructor argument:


e.g:

template<typename C>
class func {
const C& refClass;
public:
func(const C& aClass) : c(refClass) {}
double operator () (const double& x) const {
return c.(x);
^
What's the dot doing there? Did you mean to call a member function?
};
};

class myClass {
int a,b;
public:
myClass(const int A, const int B) : a(A), b(B) {}
double ez(const double& x) const {
return pow(cos(x), a) + pow(sin(x), b);
}
};

myClass mc(2,3);
func<myClass> f(mc);
for (int i=0; i<100; ++i)
std::cout << f(i*0.01) << ' ';
std::cout << std::endl;


Well is there any way to declare func such as:
func f(mc) ?

No. 'func' is a template, you need template arguments to instantiate it.

You _could_ wrap your loop in a function where 'f(mc)' is going to be one
of the arguments, and then have a helper function which will return the
object, something like

template<class C> func<C> f_helper(const C& c) { return func<C>(c); }

template<class F>
void loop_wrapper(F f)
{
for (int i = 0; i < 100; ++i)
std::cout << f(i*0.01) << " ";
}

....
myClass mc(2,3);
loop_wrapper(f_helper(mc));

V
 
S

sebbao

return c.(x);

^
What's the dot doing there? Did you mean to call a member function?

Sorry I meant "return c.ez(x);"

Thanks for the propositions. I thought the use of the class in both the
constructor and the template parameter was redundant. The wrapper idea
is one I did not think about.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top