Solved

How to use function in template declaration

Posted on 2001-06-16
11
207 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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