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 > Template and function pointer question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Template and function pointer question

Réponse
 
LinkBack Outils de la discussion
Vieux 15/10/2007, 20h09   #1 (permalink)
webinfinite@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Template and function pointer question

This is my first post here, please tell me if I did anything wrong.

in the following code snippet:
1. template <class In, class Pred>
2. In find_if(In begin, In end, Pred f) {
3. while (begin != end && !f(*begin))
4. ++begin;
5. return begin;
6. }

7. bool is_negative(int n) {
8. return n < 0;
9. }

10. vector<int>::iterator i = find_if(v.begin(), v.end(),
is_negative);

find_if is the template needs a class Pred as its third argument but
in line 10's function call, it takes a function poniter is_negative,
does that mean in C++, function point is equivalent to class?

Thank you.

  Réponse avec citation
Vieux 15/10/2007, 20h50   #2 (permalink)
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Template and function pointer question

On 2007-10-15 21:09, webinfinite@gmail.com wrote:
> This is my first post here, please tell me if I did anything wrong.
>
> in the following code snippet:
> 1. template <class In, class Pred>
> 2. In find_if(In begin, In end, Pred f) {
> 3. while (begin != end && !f(*begin))
> 4. ++begin;
> 5. return begin;
> 6. }
>
> 7. bool is_negative(int n) {
> 8. return n < 0;
> 9. }
>
> 10. vector<int>::iterator i = find_if(v.begin(), v.end(),
> is_negative);
>
> find_if is the template needs a class Pred as its third argument but
> in line 10's function call, it takes a function poniter is_negative,
> does that mean in C++, function point is equivalent to class?


No. If you rewrite the template thingie like this instead

template <typename In, typename Pred>
In find_if(In begin, In end, Pred f)

it might become a little more clear, the types of In and Pred are
determined when the code is compiled, and it does not have to be a
class, anything that is a type will do.

If we take you code and add (before line 10) the following:

struct IsNegative {
bool operator()(int n) { return n < 0; }
};

And after line 10 we add

IsNegative pred;
vector<int>::iterator i2 = find_if(v.begin(),v.end(), pred);

Now, in the first call the type of Pred in find_if() was a pointer to a
function, while in the second call the type is IsNegative. This process
where the types of the parameters are decided is called instantiation.
The template is not code that can be executed, it is just a template
describing a generic algorithm (or class), to be useful we must use this
description and fill in the parameters with real types, which created a
real function (or class). So with the additions that I made your program
will now contain two find_if functions, the first has the following
signature:

typedef vector<int>::iterator iter;
iter find_if(iter, iter, bool (*)(int));

Notice that the last argument is a function pointer. The second find_if
function have this signature:

iter find_if(iter, iter, IsNegative);

The last argument is now an instance of the IsNegative class.

Hope this clear some things up for you.

--
Erik Wikström
  Réponse avec citation
Vieux 16/10/2007, 09h14   #3 (permalink)
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Template and function pointer question

On Oct 15, 9:50 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2007-10-15 21:09, webinfin...@gmail.com wrote:


> > This is my first post here, please tell me if I did anything wrong.


> > in the following code snippet:
> > 1. template <class In, class Pred>
> > 2. In find_if(In begin, In end, Pred f) {
> > 3. while (begin != end && !f(*begin))
> > 4. ++begin;
> > 5. return begin;
> > 6. }


> > 7. bool is_negative(int n) {
> > 8. return n < 0;
> > 9. }


> > 10. vector<int>::iterator i = find_if(v.begin(), v.end(),
> > is_negative);


> > find_if is the template needs a class Pred as its third argument but
> > in line 10's function call, it takes a function poniter is_negative,
> > does that mean in C++, function point is equivalent to class?


> No. If you rewrite the template thingie like this instead


> template <typename In, typename Pred>
> In find_if(In begin, In end, Pred f)


> it might become a little more clear, the types of In and Pred are
> determined when the code is compiled, and it does not have to be a
> class, anything that is a type will do.


Anything which fulfills the constraints of the template. In
this case, any type which can be called ("()" operator) with a
single argument of the iterator value type, and returns
something which converts to bool.

For various historical reasons, the () operator can be applied
to a pointer to a function, as well as to a function. In
pre-template days, this was often considered a defect in the
language---a flaw in the type system. Serendipitously, it turns
out to be an advantage with templates, where duck typing is the
rule.

And of course, in C++, a pointer to a function type is an object
type, just like a class.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  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 11h32.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10616 seconds with 11 queries