PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Templates functions as template parameters
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Templates functions as template parameters

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 21h42   #1
tygro
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Templates functions as template parameters

Hi,
I would like to do something like this:

class Type
{
public:

template <template<typename> class Fun>
void applyOnType(Fun f, void* data)
{
switch(type) {
case 0:
f<int>(data);
break;
case 1:
f<short>(data);
break;
}
};

Then I could call:
template <typename T> void myFunction(T* data) { }

DataType dt;
dt.applyOnType(myFunction, data);


But it doesn't work because I guess it is not possible to pass a
template function in argument to a function.
Furthermore, because of the template-template, it seems that Fun must be
functor.
Do you see a way to write something as flexible as this but which could
compile?

My only working solution was this:
class TypeFunc
{
public:
template<typename T> void operator()() {}
};

void onDataType(TypeFunc& f, void* data)
{
switch(..) {
...
f.operator()<int>();
...
}
}

I don't think it's easy to use. I also have a macro based solution, but
I would prefer something 100% template.

Thank you for your .
  Réponse avec citation
Vieux 25/02/2008, 21h51   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Templates functions as template parameters

tygro wrote:
> Hi,
> I would like to do something like this:
>
> class Type
> {
> public:
>
> template <template<typename> class Fun>
> void applyOnType(Fun f, void* data)
> {
> switch(type) {


What's "type"? It's not defined here.

> case 0:
> f<int>(data);
> break;
> case 1:
> f<short>(data);
> break;
> }
> };
>
> Then I could call:
> template <typename T> void myFunction(T* data) { }
>
> DataType dt;
> dt.applyOnType(myFunction, data);
>
>
> But it doesn't work because I guess it is not possible to pass a
> template function in argument to a function.
> Furthermore, because of the template-template, it seems that Fun must
> be functor.


A function is a functor. No big deal.

> Do you see a way to write something as flexible as this but which
> could compile?


Flexible? Looks like too flexible.

>
> My only working solution was this:
> class TypeFunc
> {
> public:
> template<typename T> void operator()() {}
> };
>
> void onDataType(TypeFunc& f, void* data)
> {
> switch(..) {
> ...
> f.operator()<int>();
> ...
> }
> }
>
> I don't think it's easy to use. I also have a macro based solution, but I
> would prefer something 100% template.


Try looking in the FAQ first. Then if you have some questions
remainging unanswered, come back and ask again. Start with 5.8,
then move onto section 35 (templates).

Also, perhaps you could explain what you're trying to accomplish
with design like yours.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 25/02/2008, 22h11   #3
tygro
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Templates functions as template parameters

Thank you for the quick response.

Victor Bazarov wrote:
> A function is a functor. No big deal.

So maybe I'm doing something bad. Can you explain this:
template <typename T>
class testFunctor
{
public:
void operator()() {}
};

template <typename T> void purFunction() {}

template <template<typename> class Fun>
void dummy()
{
Fun<int>();
}

int main()
{
dummy<testFunctor>();
dummy<purFunction>();

return 0
}

dummy<purFunction>() won't compile.


>> Do you see a way to write something as flexible as this but which
>> could compile?

>
> Flexible? Looks like too flexible.

Not really.

>>
>> I don't think it's easy to use. I also have a macro based solution, but I
>> would prefer something 100% template.

>
> Try looking in the FAQ first. Then if you have some questions
> remainging unanswered, come back and ask again. Start with 5.8,
> then move onto section 35 (templates).

I have already looked at the FAQ and a lot of books.

> Also, perhaps you could explain what you're trying to accomplish
> with design like yours.

I have a void* and a type as an enum. There is nothing to do against
this fact.
So I want to be able to write template functions which will be instanced
according to type.

> V

  Réponse avec citation
Vieux 25/02/2008, 22h39   #4
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Templates functions as template parameters

tygro wrote:
> Thank you for the quick response.
>
> Victor Bazarov wrote:
>> A function is a functor. No big deal.

> So maybe I'm doing something bad. Can you explain this:
> template <typename T>
> class testFunctor
> {
> public:
> void operator()() {}
> };
>
> template <typename T> void purFunction() {}
>
> template <template<typename> class Fun>

^^^^^
Here's your "culprit". You said you'll be passing a template
of a class (i.e. a class template). A function template is
not a class template.

> void dummy()
> {
> Fun<int>();
> }
>
> int main()
> {
> dummy<testFunctor>();
> dummy<purFunction>();
>
> return 0
> }
>
> dummy<purFunction>() won't compile.


You need to look at the 'std::tr1::function'. I am not sure how to
use it, but I bet you that's where you'll find what you're looking for.

> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 26/02/2008, 10h19   #5
John Gilson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Templates functions as template parameters

On Feb 25, 1:42 pm, tygro <hubrys.neme...@gmail.com> wrote:
> Hi,
> I would like to do something like this:
>
> class Type
> {
> public:
>
> template <template<typename> class Fun>
> void applyOnType(Fun f, void* data)
> {
> switch(type) {
> case 0:
> f<int>(data);
> break;
> case 1:
> f<short>(data);
> break;
> }
>
> };
>
> Then I could call:
> template <typename T> void myFunction(T* data) { }
>
> DataType dt;
> dt.applyOnType(myFunction, data);
>
> But it doesn't work because I guess it is not possible to pass a
> template function in argument to a function.
> Furthermore, because of the template-template, it seems that Fun must be
> functor.
> Do you see a way to write something as flexible as this but which could
> compile?
>
> My only working solution was this:
> class TypeFunc
> {
> public:
> template<typename T> void operator()() {}
>
> };
>
> void onDataType(TypeFunc& f, void* data)
> {
> switch(..) {
> ...
> f.operator()<int>();
> ...
> }
>
> }
>
> I don't think it's easy to use. I also have a macro based solution, but
> I would prefer something 100% template.
>
> Thank you for your .


You're on the right track. You can't pass a function template as an
argument (a function template is a template but not a function) but
you can pass an object whose type is a class that has defined an
appropriate member function template:

template <typename T> void myFunction(T * data);

class TypeFunc
{
public:
template<typename T> void operator()(T * data) const
{
myFunction(data);
}
};

class Type
{
public:
template<typename F>
void applyOnType(const F & f, void * data)
{
switch(type) // Assume "type" is defined in scope
{
case 0:
f(static_cast<int *>(data));
break;
case 1:
f(static_cast<short *>(data));
break;
// ...
}
}
};

Type t;
void * data = ...;
t.applyOnType(TypeFunc(), data);

-- JAG
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13709 seconds with 13 queries