Topic: member template instantiated on self?


Author: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/04/26
Raw View
"TiTi" <cenTipod@anTi.com> wrote in message
news:8dp1hl$a7t$1@trex.antw.online.be...
> Travis Vitek <vitek@engr.orst.edu> wrote in message
> news:Pine.GSO.4.10.10004201723340.7927-
> > Is the following code legal? It appears to work on many compilers, but
> > fails on others...
>
>
> I'm not entirely sure, but I think that compilers should fail on the
> program. You have a member List<X> in your X class. At this point, class X
> isn't fully defined yet. Your compiler then tries to compile List<X>, X
> being the template parameter, *but* with X being a type that is not
> complete. The compiler tries to produce code for struct Node and fails
I have given an example where it is right. But the original code does not
contain a _Ty value. Please, take a closer look at List: it does not have
instances of _Ty type. _Ty is used in the struct Node declaration only and
_Ty should not be a complete type to bring to being struct Node *type*.
The situation is changed if you create an object of _Ty type in the List.
I.e. if you do something like this:
template< class _Ty >
 class List {
 private:
   struct Node { Node* next_; _Ty value_; } s_; // s_ object requires _Ty to
be a complete type
   Node* n_; // n_ does not require _Ty to be a complete type
};

> because Node has a member that is of class X. That is impossible to have
The type Node has a member that is of class X But the class List does not
have a member of type _Ty, i.e. of class X.

With regards,
Michael Kochetkov.

> because X is not complete.
>
>
> [...]
>
> TiTi
> JG
> --
> "Damn you Murphy!" - TiTi
> For comp.lang.c++:
>   * Read Shiva's "Welcome to comp.lang.c++" before posting to that
> newsgroup:
>    http://www.slack.net/~shiva/welcome.txt
>   * FAQ (frequently asked questions) :
> http://marshall-cline.home.att.net/cpp-faq-lite/
>
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/04/21
Raw View
IMO your compiler is wrong.
struct Node { Node* next_; _Ty value_; }; -- is a declaration only and _Ty
does not have to be a complete  type to bring such a declaration to being.
But it should be a complete type for X x instantiation. And it really is a
complete type at this very moment in main function indeed.
Consider the following comparison:
 template< class _Ty >
 class List {
 private:
   struct Node { Node* next_; _Ty value_; } s_;
};

s_ requires _Ty to be a complete type now. And this will not compile.

With regards,
Michael Kochetkov.

"Travis Vitek" <vitek@engr.orst.edu> wrote in message
news:Pine.GSO.4.10.10004201723340.7927-100000@eel.ENGR.ORST.EDU...
>
> Is the following code legal? It appears to work on many compilers, but
> fails on others...
>
> template< class _Ty >
> class List {
> private:
>   struct Node { Node* next_; _Ty value_; }; // <- !!HERE!!
> file://private:
> //  Node* front_;
> //  Node* back_;
> };
>
> file://class X;
> file://template List<X>;
> file://typedef List<X> XList;
>
> class X {
> private:
>   List<X> list_;
> };
>
> int main(void) {
>   X x;
>   return(0);
> }
>
>
> The HP aCC compiler gives this message:
>
> Error 239: "foo.cpp", line 4 # A class/union shall not contain a member
> with incomplete type.
>       struct Node { Node* _next; _Ty _value; };
>                                      ^^^^^^
> Error 239: "foo.cpp", line 4 # A class/union shall not contain a member
> with incomplete type.
>       struct Node { Node* _next; _Ty _value; };
>                                      ^^^^^^
> Error 537: "foo.cpp", line 4 # Cannot create a 'X' object; class X has
> only been seen as an incomplete declaration.
>       struct Node { Node* _next; _Ty _value; };
>                                      ^^^^^^
> Please respond via e-mail...
>
> Travis
>
>
> *********************************************************************
> ***  Travis Vitek  ***  Senior Computer Science/Mathematics  ********
> *********************************************************************
>
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "TiTi" <cenTipod@anTi.com>
Date: 2000/04/22
Raw View
Travis Vitek <vitek@engr.orst.edu> wrote in message
news:Pine.GSO.4.10.10004201723340.7927-
> Is the following code legal? It appears to work on many compilers, but
> fails on others...


I'm not entirely sure, but I think that compilers should fail on the
program. You have a member List<X> in your X class. At this point, class X
isn't fully defined yet. Your compiler then tries to compile List<X>, X
being the template parameter, *but* with X being a type that is not
complete. The compiler tries to produce code for struct Node and fails
because Node has a member that is of class X. That is impossible to have
because X is not complete.


[...]

TiTi
JG
--
"Damn you Murphy!" - TiTi
For comp.lang.c++:
  * Read Shiva's "Welcome to comp.lang.c++" before posting to that
newsgroup:
   http://www.slack.net/~shiva/welcome.txt
  * FAQ (frequently asked questions) :
http://marshall-cline.home.att.net/cpp-faq-lite/


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]