Topic: nested classes: same type or not


Author: jeffturneresq@my-deja.com
Date: 2000/04/15
Raw View
In article <38CF4796.45ECA25@era.ericsson.se>,
  Tom Bjorkholm <tom.bjorkholm@era.ericsson.se> wrote:
> Hello Gurus,
>
> Our library vendor has one opinion of what is legal
> ISO C++. I have another opinion. Our (small) compiler
> vendor shares my opinion. Could you please help us
> determine what is correct?
>
> //------ code snippet begins ---------
>
> void * allocate(size_t s) { return new char[s];}
>
> template <typename T> class A
> {
> public:
>    struct B
>    {
>      void *alloc(size_t s) {return allocate(s);}
>    };
>    // other members of A ...
> };
>
> A<int>::B bi;
> A<char>::B bc; // does bi and bc have the same static type?
>
> bi = bc; // is assignment allowed
>          // without defining assignment or conversion?
>
> //------ code snippet ends ---------
>
> My understanding is that A<int>::B and A<char>::B are
> different static types. I understand that they are
> totally unrelated types, despite the fact that they
> happen have exactly the same operations and behavior.
> My understanding is also that it is not allowed to use
> assignment between these types. My small compiler
> vendor has implemented this correctly according to my
> understanding.
>
> One of the biggest C++ library vendors claim that
> A<int>::B and A<char>::B are actually the same type.

I'm not sure about the standard, but in C it is quite common for
compilers to upgrade chars to ints when they feel like it.  This
has probably been inherited by C++ to some extent (compatability
and all that).  So I would guess that either implementation may
be correct - a loophole for compiler writers.

--Jeff Turner
using std::disclaimer


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/04/15
Raw View
On Sat, 15 Apr 2000 11:38:17 CST, jeffturneresq@my-deja.com wrote:

>In article <38CF4796.45ECA25@era.ericsson.se>,
>  Tom Bjorkholm <tom.bjorkholm@era.ericsson.se> wrote:
>> Hello Gurus,
>>
>> Our library vendor has one opinion of what is legal
>> ISO C++. I have another opinion. Our (small) compiler
>> vendor shares my opinion. Could you please help us
>> determine what is correct?
>>
>> //------ code snippet begins ---------
>>
>> void * allocate(size_t s) { return new char[s];}
>>
>> template <typename T> class A
>> {
>> public:
>>    struct B
>>    {
>>      void *alloc(size_t s) {return allocate(s);}
>>    };
>>    // other members of A ...
>> };
>>
>> A<int>::B bi;
>> A<char>::B bc; // does bi and bc have the same static type?
>>
>> bi = bc; // is assignment allowed
>>          // without defining assignment or conversion?
>>
>> //------ code snippet ends ---------
>>
>> My understanding is that A<int>::B and A<char>::B are
>> different static types. I understand that they are
>> totally unrelated types, despite the fact that they
>> happen have exactly the same operations and behavior.
>> My understanding is also that it is not allowed to use
>> assignment between these types. My small compiler
>> vendor has implemented this correctly according to my
>> understanding.
>>
>> One of the biggest C++ library vendors claim that
>> A<int>::B and A<char>::B are actually the same type.
>
>I'm not sure about the standard, but in C it is quite common for
>compilers to upgrade chars to ints when they feel like it.

You are probably thinking of what happens to char values when used in
expressions. In both C and C++, a char value is promoted to type int
when used in an expression. Promotion of values in expressions has
nothing to do with the meaning of declared types.

A<int>::B and A<char>::B are certainly different types. Type identity
in C++ is based on names, not on content. For example, given
 struct { int i; } x;
 struct { int i; } y;
variables x and y have different (anonymous) types, and no other
object or type in the program can have the same type as x or y. This
rule is the same in C and C++.

---
Steve Clamage, stephen.clamage@sun.com

---
[ 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: kanze@gabi-soft.de
Date: 2000/03/28
Raw View
Martin von Loewis <loewis@informatik.hu-berlin.de> writes:

|>  Tom Bjorkholm <tom.bjorkholm@era.ericsson.se> writes:

|>  > My understanding is that A<int>::B and A<char>::B are
|>  > different static types.

|>  That is my understanding as well. I had some problems finding the
|>  exact wording in the standard that says so, all I have

|>  # A member function, a member class, or a static data member of a
|>  # class template instantiated from the member definition of the class
|>  # template is called, respectively, an instantiated member function,
|>  # member class or static data member.

I don't think that this passage is relevant -- it only concerns member
templates.  However, I don't see why the standard should say anything
directly; a class template defines a mechanism for creating classes, but
the classes it creates are distinct.  Since the two outer classes are
unrelated, their inner classes are also unrelated.

--
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ 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: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 2000/03/30
Raw View
kanze@gabi-soft.de writes:

> I don't think that this passage is relevant -- it only concerns
> member templates.  However, I don't see why the standard should say
> anything directly; a class template defines a mechanism for creating
> classes

Where does it say so? All I found is 14/1

# A template defines a family of classes or functions.

That does not answer the specific questions what the family of classes
is, i.e. whether nested classes of template instantiations are the
same or different classes.

> Since the two outer classes are unrelated, their inner classes are
> also unrelated.

Well, they are not unrelated - they are in the same family. So that
still does not answer the question... I agree, of course, that the
nested classes should be "unrelated" - I just couldn't back-up that in
the standard.

The reference to 3.5/9 is convincing, though: since A<int> and A<char>
are different, their member types are also different.

Regards,
Martin

---
[ 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: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 2000/03/16
Raw View
Tom Bjorkholm <tom.bjorkholm@era.ericsson.se> writes:

> My understanding is that A<int>::B and A<char>::B are
> different static types.

That is my understanding as well. I had some problems finding the
exact wording in the standard that says so, all I have

# A member function, a member class, or a static data member of a
# class template instantiated from the member definition of the class
# template is called, respectively, an instantiated member function,
# member class or static data member.

So it is clear that A<int>::B and A<char>::B both are "instantiated
member classes". The question is whether they are the same
class. However, consider

template <typename T> class A
{
public:
   struct B
   {
     T x;
   };
};


Here, the layout of A<int>::B is clearly different from A<char>::B, so
the should be different types. Unless there is specific wording in the
standard that says that your case is different from my example, I'd
assume that they should be treated equivalent.

Regards,
Martin

---
[ 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: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: 2000/03/16
Raw View
Martin von Loewis <loewis@informatik.hu-berlin.de> writes:

| Tom Bjorkholm <tom.bjorkholm@era.ericsson.se> writes:
|
| > My understanding is that A<int>::B and A<char>::B are
| > different static types.
|
| That is my understanding as well. I had some problems finding the
| exact wording in the standard that says so, all I have

C++ uses name equivalence for deciding type-equivalence. So A<int>::B
and A<char>::B are *distinct* types.  See 3.5/9.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/16
Raw View
In article <38CF4796.45ECA25@era.ericsson.se>, Tom Bjorkholm <tom.bjorkh
olm@era.ericsson.se> writes
>What has the standard got to say about this?
>
Naming and shaming sort of springs to mind.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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: Tom Bjorkholm <tom.bjorkholm@era.ericsson.se>
Date: 2000/03/16
Raw View
Hello Gurus,

Our library vendor has one opinion of what is legal
ISO C++. I have another opinion. Our (small) compiler
vendor shares my opinion. Could you please help us
determine what is correct?

//------ code snippet begins ---------

void * allocate(size_t s) { return new char[s];}

template <typename T> class A
{
public:
   struct B
   {
     void *alloc(size_t s) {return allocate(s);}
   };
   // other members of A ...
};

A<int>::B bi;
A<char>::B bc; // does bi and bc have the same static type?

bi = bc; // is assignment allowed
         // without defining assignment or conversion?

//------ code snippet ends ---------

My understanding is that A<int>::B and A<char>::B are
different static types. I understand that they are
totally unrelated types, despite the fact that they
happen have exactly the same operations and behavior.
My understanding is also that it is not allowed to use
assignment between these types. My small compiler
vendor has implemented this correctly according to my
understanding.

One of the biggest C++ library vendors claim that
A<int>::B and A<char>::B are actually the same type.

What has the standard got to say about this?

Thank you,
/Tom
--
-------------------------------------------------------------
Tom Bjorkholm   Ericsson Radio Systems AB Tel. +46-8-4221348
                Nacka Strand,             Fax. +46-8-4220540
                S-131 89 Stockholm, Sweden

---
[ 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              ]