Solved

How to use function in template declaration

Posted on 2001-06-16
11
203 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now