C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Curiously reccuring template patterns, et types

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Laurent Rineau
Guest





PostPosted: Mon Aug 23, 2004 10:02 pm    Post subject: Curiously reccuring template patterns, et types Reply with quote



(Ceci est mon premier message dans fr.comp.lang.c, et même sur Usenet en
général.)

On peut faire ce genre de choses en C++ :

template <typename Model>
struct Concept {

Model& self()
{
return static_cast<Model&>(*this);
}

void f()
{
self.f_impl();
}
};

struct Model: public Concept<Model>
{
void f_impl() {};
};

Ainsi, Model::f() est une fonction de la superclass Concept dont le
comportement dépend de la classe dérivée Model. C'est décrit dans [1] (si
d'ailleurs quelqu'un peut me dire comment lire ce genre de chose, il faut
être abonné ?).

Bizarrement, on ne peut pas faire en sorte que Concept utilise un type
défini dans Model :

template <typename Model>
struct Concept {

Model& self()
{
return static_cast<Model&>(*this);
}

typedef typename Model::f_return_type f_type; // ligne 9

f_type f()
{ // ligne 12
return self.f_impl();
}
};

struct Model: public Concept<Model> // ligne 17
{ // ligne 18
typedef int f_return_type;

f_return_type f_impl() { return 0; };
};

J'obtiens les erreurs suivantes :

essai2.C: In instantiation of `Concept<Model>':
essai2.C:18: instantiated from here
essai2.C:9: invalid use of undefined type `struct Model'
essai2.C:17: forward declaration of `struct Model'
essai2.C:12: invalid use of undefined type `struct Model'
essai2.C:17: forward declaration of `struct Model'

Quelqu'un sait-il pourquoi exactement ? La définition de Model provoque
l'instanciation de Concept<Model>. Alors que, bizarrement, le fait que
Model::f_impl() soit défini après la ligne 18 ne gène pas le compilateur,
le fait que Model::f_return_type le soit semble provoquer
l'erreur. Pourquoi cette différence ?

[1] Coplien J.: "Curiously reccuring template patterns", The C++ Report,
Vol.7, No.2, Feb.1995, pp.24-27.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.