Solved

How to use function in template declaration

Posted on 2001-06-16
11
206 Views
Last Modified: 2010-04-02
I'm trying to write a template class that can be used with different functions instead of different classes.

template<class T>
class bravo
{
bravo(int x)
{
T(x);
data = x;
}
int data;
};

Say I had the above template class, but instead of <class T> I want something like <function T>.  How can I do this?
0
Comment
Question by:prgrmmer93231
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 6197737
You can use a function object.
Example:
class Add3Function
{
public:
     int operator()(int x)const {return x+3;}
};


template<class T>
class foo
{
public:
     foo(int x)
     {
          T f;
          data = f(x);
     };
     int data;
};


int main(int argc, char* argv[])
{
     foo<Add3Function> SomeFoo(4);
     int x = SomeFoo.data;
     return 0;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 6197747
Correction:
template<class T>
class foo
{
public:
     foo(int x)
     {
          data = T()(x);
     };
     int data;
};
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6197790
No, I think you first example was syntactically correct, it is

int x;
T f;
f(x);

and /not/
f()(x);

This allows you to use both function pointers as well as classes with operator().
Function classes are more powerfull then function pointers as they work with objects. Objects allow information being held in between 'function' calls:

template <typename T>
class U
{
   T foo;

   public:
      void bar(int x)
      {
         foo(x);
      }
};
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Expert Comment

by:Andrey_Kulik
ID: 6197958
Simple example using functional's

#include <functional>
#include <iostream>

using namespace std;

// simple function
int foo(int x) { return x * 2; }

// simple class
class A
{
public:
     int foo(int x) { return x + 2; }
};

// your class
class Test
{
public:
     template<class T>
     void Run(int param, T t)
     {
          cout << "Param = " << param << " | result = " << t(param) << endl;
     }
};

void main(void)
{
     Test t;
     A a;
     t.Run(10, ptr_fun(foo));
     t.Run(10, bind1st(mem_fun1(&A::foo), &a));
}

Best regards
Andrey
0
 
LVL 2

Expert Comment

by:Andrey_Kulik
ID: 6197965
IMHO my example is more flexible :)
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6198382
It's..... different ;)

Function objects are indeed also used as parameters to template functions. The STL is crammed with algorithm implementations that take those as a template argument.

>> bind1st(mem_fun1(&A::foo), &a)
Interesting, I think I have to go and decipher some STL code....

Nice example Andrey!
0
 

Author Comment

by:prgrmmer93231
ID: 6198491
Andrey_Kulik,
I tried your method, but I got a compile error for ptr_fun.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6198498
Did you put the #include <functional>
And did you either put using namespace std;
Or std::ptr_fun
0
 

Author Comment

by:prgrmmer93231
ID: 6198561
Axter,
You're right.  I forgot to add the std:: prefix.

KangaRoo,
Thanks for your input.

Andrey_Kulik,
I tried both methods, but I like the first method posted better.

At one point or another, I still might use your method, so I like to give you some points for it.
I'll post a new 50 point question for you.  That's the last of my points.

I have one more question related to this. What is the difference between using "typename" and "class" in the template, and is there anything else you can put there?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6199362
There is no real difference, I just like 'typename' better because template arguments can be any type and not just classes.
0
 

Author Comment

by:prgrmmer93231
ID: 6200467
Thanks KangaRoo
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question