?
Solved

How to use function in template declaration

Posted on 2001-06-16
11
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

765 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