By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

parameter deduction in function object

P: n/a
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) ?
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 29 Mar 2005 08:39:22 -0800 in comp.lang.c++, se****@yahoo.fr
(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.

Jul 23 '05 #2

P: n/a
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:
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
Jul 23 '05 #3

P: n/a
> 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.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.