Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > parameter deduction in function object

Reply
Thread Tools

parameter deduction in function object

 
 
Marcel Sebbao
Guest
Posts: n/a
 
      03-29-2005
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) ?
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      03-29-2005
On 29 Mar 2005 08:39:22 -0800 in comp.lang.c++,
(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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-29-2005
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
 
Reply With Quote
 
sebbao@yahoo.fr
Guest
Posts: n/a
 
      03-29-2005
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function template type deduction from default parameter tobias.loew@steag.com C++ 2 01-08-2013 07:53 AM
function pointer as template parameter + type deduction er C++ 5 06-09-2009 02:04 AM
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
template class instantiate without template parameter, automatictype deduction Fei Liu C++ 0 10-25-2007 08:12 PM
Template parameter Deduction Neelesh C++ 4 11-10-2005 12:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57