Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use function in template declaration

Posted on 2001-06-16
11
Medium Priority
?
216 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

604 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