Topic: Clarification required


Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: Wed, 4 Apr 2001 17:47:04 GMT
Raw View
I have two questions to ask:

1. Does the standard guarantee that sizeof(some_type) has the same value in
all translation units?
2. Does the standard guarantee that sizeof(&X::f) == sizeof(&Y::g) is true for
any clases X, Y and any non-static member functions f, g? (And the same
question about pointers to data members).

PS This questions follow the "Pointer to member function" thread in
comp.lang.c++.moderated.
--
         With all respect, Sergey.          http://cpp3.virtualave.net/
         mailto : ders at skeptik.net

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@stop.mail-abuse.org>
Date: Wed, 4 Apr 2001 14:24:43 CST
Raw View

"Sergey P. Derevyago" wrote:
>
> I have two questions to ask:
>
> 1. Does the standard guarantee that sizeof(some_type) has the same value in
> all translation units?

Depends what you mean by some_type.  If the type is trully the same, then the
requirement is that their be only one definition, which can not lead to more
than one size.

Of course, if some_type is merely an indicator of a name, it doesn't even hold
true for a single translation unit:

 int main() {
     {
  struct x { int a; };
  cout << sizeof (some_type) << "\n";
     }
     {
  struct some_type { int a, b; };
  cout << sizeof (some_type) << "\n";
     }
 }

> 2. Does the standard guarantee that sizeof(&X::f) == sizeof(&Y::g) is true for
> any clases X, Y and any non-static member functions f, g? (And the same
> question about pointers to data members).

Certainly not, and in current implementations you frequently see this.
Sometimes pointer-to-members are twice as big as they are other times.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Jeff Peil" <jpeil@bigfoot.com>
Date: Thu, 5 Apr 2001 00:41:39 GMT
Raw View
"Ron Natalie" <ron@stop.mail-abuse.org> wrote in message
news:3ACB701C.193320A4@stop.mail-abuse.org...
>
> Certainly not, and in current implementations you frequently see this.
> Sometimes pointer-to-members are twice as big as they are other times.
>

In practice there are compilers where the sizes can vary, and of course as
Sergey didn't specify that &X::f and &Y::g were either both pointers to
member data or pointers to member functions, they certainly could vary.
However I think it would be pretty difficult for a conforming implementation
to use differing sizes for pointer to members in different classes (data
member pointers and function member pointers can each have differing sizes
wihtout a problem but I can't see an easy way for a conformant
implementation to have varying sizes between different pointer to member
function types or between different pointer to data types.)

The problem is with the requirements in 5.2.10 item 9 for pointers to member
functions <converting an rvalue of type "pointer to member function" to a
different pointer to member function type and back to its original type
yields the original pointer to member value.> So in a conformant
implementation it must support casting between any two pointer to member
function types and back losslessly (if the sizes of two different pointer to
member function types differed then casting from the larger type to the
smaller type, storing in an intermediate varaible and casting back would
make it .

Similarly 5.2.10 item 9 places the requirement on pointers to memeber data
<converting an rvalue of type "pointer to data member of X of type T1" to
the type "pointer to data mem-ber of Y of type T2" (where the alignment
requirements of T2 are no stricter than those of T1) and back to its
original type yields the original pointer to member value.> As with the
pointer to member function case this requirement would make it pretty
difficult for a conformant implementation to have different sizes for
differtent pointers to data member types (because as long as T1 and T2 are
the same type, converting between pointer to data member of type Y and
pointer to data member of type X needs to be a lossless conversion so that
the original value can be restored if the pointer is converted back.).


---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "TiTi" <titi_@skynet.be>
Date: Fri, 6 Apr 2001 12:32:34 CST
Raw View
> I have two questions to ask:
>
> 1. Does the standard guarantee that sizeof(some_type) has the same value
in
> all translation units?

Well, Any definition of a type has to obey the 'one definition rule', and
that answers your question.

> 2. Does the standard guarantee that sizeof(&X::f) == sizeof(&Y::g) is true
for
> any clases X, Y and any non-static member functions f, g? (And the same
> question about pointers to data members).

I'm not entirely sure about this, but what I am sure about is that the size
of a common pointer may differ from a pointer to member function.

> PS This questions follow the "Pointer to member function" thread in
> comp.lang.c++.moderated.


TiTi


---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: brangdon@cix.co.uk (Dave Harris)
Date: Sat, 7 Apr 2001 18:13:12 GMT
Raw View
ron@stop.mail-abuse.org (Ron Natalie) wrote (abridged):
> > 2. Does the standard guarantee that sizeof(&X::f) == sizeof(&Y::g)
> > is true for any clases X, Y and any non-static member functions f,
> > g? (And the same question about pointers to data members).
>
> Certainly not, and in current implementations you frequently see this.
> Sometimes pointer-to-members are twice as big as they are other times.

Really? When and why does the size vary? Can you elaborate in the context
of a program like:

    #include <iostream>

    class MyClass;
    typedef void (MyClass::*PMF)();
    const int a = sizeof PMF;

    class MyClass {
    public:
         void func();
    };
    const int b = sizeof &MyClass::func;

    int main() {
        std::cout << a << ' ' << b;
    }

Here MyClass is a partial type when a is defined. Are you saying that the
value of a is different to b? If not, how does the compiler ascribe a
value to sizeof PMF when it knows so little about what it points to?

  Dave Harris, Nottingham, UK | "Weave a circle round him thrice,
      brangdon@cix.co.uk      |   And close your eyes with holy dread,
                              |  For he on honey dew hath fed
 http://www.bhresearch.co.uk/ |   And drunk the milk of Paradise."

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]