Topic: sizeof( class member )


Author: "Peter Himmelbauer" <phi@teleweb.at>
Date: 2000/09/26
Raw View
Hmm, looks to me as returning some reeeeally big number
instead of the expected "4" (on 32-bit platforms).

Peter



Kurt Krueckeberg schrieb in Nachricht ...
>int is a built-int type(not a struct of class). Use
>numeric_limits<int>::max()  // #include <limits> and be sure to do: using
>namespace std;
>
>--kurt
>
>"Balog Pal" <pasa@lib.hu> wrote in message
>news:000001c02424$2b5afde0$980e38c3@bpnt...
>> In msvc5 I tried something like this:
>>
>> class foo
>> {
>>  private:
>>    int x;
>>  public
>>    enum {reserve = sizeof(x)};
>> };
>>
>> That had me an error:
>> "Compiler Error C2327
>> 'symbol' : member from enclosing class is not a type name, static, or an
>> enumerator
>>
>> >From within a nested class, you attempted to access a member of the
>> enclosing class that was not a type name, a static member, or an
>> enumerator."
>>
>> Is that the intended behavior? And if yes, why is that good? The example
>> coming with an error makes real access to the enclosing class' member,
>that
>> is obviously an error, as the inner class can't have a 'this' for access.
>> But sizeof() does not need anything like that, and could very well work.
>>
>> What is even more interesting, I tried amember function:
>>
>> static int GetReserve { return sizeof(x);}
>>
>> and that produced
>> "Compiler Error C2070
>> illegal sizeof operand
>>
>> The operand of a sizeof expression was not an expression or a type name."
>>
>> Removing static it compiles.
>> to make it work as static a nightmare like
>>
>> static int GetReserve { return sizeof( ((foo*)0)->x);}
>>
>> is needed. Too bad that is not recognized by the compiler as a 'constant
>> expression', highly limiting the use. And trying the same trick on enum
>> doesn't work, as the compiler reports foo * as undefined type. BAH. (and
>> initialized consts are not yet supported in this compiler).
>>
>>
>> Paul
>>
>>
>>
>>
>>
>> ---
>> [ 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              ]
>

---
[ 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: "Peter Himmelbauer" <phi@teleweb.at>
Date: 2000/09/26
Raw View
Try

sizeof(foo::x)

Peter


Balog Pal schrieb in Nachricht <000001c02424$2b5afde0$980e38c3@bpnt>...
>In msvc5 I tried something like this:
>
>class foo
>{
> private:
>   int x;
> public
>   enum {reserve = sizeof(x)};
>};
>
>That had me an error:
>"Compiler Error C2327
>'symbol' : member from enclosing class is not a type name, static, or an
>enumerator
>
>>From within a nested class, you attempted to access a member of the
>enclosing class that was not a type name, a static member, or an
>enumerator."
>
>Is that the intended behavior? And if yes, why is that good? The example
>coming with an error makes real access to the enclosing class' member, that
>is obviously an error, as the inner class can't have a 'this' for access.
>But sizeof() does not need anything like that, and could very well work.
>
>What is even more interesting, I tried amember function:
>
>static int GetReserve { return sizeof(x);}
>
>and that produced
>"Compiler Error C2070
>illegal sizeof operand
>
>The operand of a sizeof expression was not an expression or a type name."
>
>Removing static it compiles.
>to make it work as static a nightmare like
>
>static int GetReserve { return sizeof( ((foo*)0)->x);}
>
>is needed. Too bad that is not recognized by the compiler as a 'constant
>expression', highly limiting the use. And trying the same trick on enum
>doesn't work, as the compiler reports foo * as undefined type. BAH. (and
>initialized consts are not yet supported in this compiler).
>
>
>Paul
>
>
>
>
>
>---
>[ 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: wmm@fastdial.net
Date: 2000/09/25
Raw View
In article <000001c02424$2b5afde0$980e38c3@bpnt>,
  "Balog Pal" <pasa@lib.hu> wrote:
> In msvc5 I tried something like this:
>
> class foo
> {
>  private:
>    int x;
>  public
>    enum {reserve = sizeof(x)};
> };
>
> That had me an error:
> "Compiler Error C2327
> 'symbol' : member from enclosing class is not a type name, static, or
an
> enumerator
>
> >From within a nested class, you attempted to access a member of the
> enclosing class that was not a type name, a static member, or an
> enumerator."
>
> Is that the intended behavior? And if yes, why is that good? The
example
> coming with an error makes real access to the enclosing class'
member, that
> is obviously an error, as the inner class can't have a 'this' for
access.
> But sizeof() does not need anything like that, and could very well
work.

This is an open issue on the core language issues list.  The
wording of the Standard on this question is ambiguous at best.
There's discussion about clarifying it to permit references to
nonstatic class members in "unused" contexts like sizeof and
non-polymorphic typeid (see 3.2).

--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)


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: "Balog Pal" <pasa@lib.hu>
Date: 2000/09/22
Raw View
In msvc5 I tried something like this:

class foo
{
 private:
   int x;
 public
   enum {reserve = sizeof(x)};
};

That had me an error:
"Compiler Error C2327
'symbol' : member from enclosing class is not a type name, static, or an
enumerator

>From within a nested class, you attempted to access a member of the
enclosing class that was not a type name, a static member, or an
enumerator."

Is that the intended behavior? And if yes, why is that good? The example
coming with an error makes real access to the enclosing class' member, that
is obviously an error, as the inner class can't have a 'this' for access.
But sizeof() does not need anything like that, and could very well work.

What is even more interesting, I tried amember function:

static int GetReserve { return sizeof(x);}

and that produced
"Compiler Error C2070
illegal sizeof operand

The operand of a sizeof expression was not an expression or a type name."

Removing static it compiles.
to make it work as static a nightmare like

static int GetReserve { return sizeof( ((foo*)0)->x);}

is needed. Too bad that is not recognized by the compiler as a 'constant
expression', highly limiting the use. And trying the same trick on enum
doesn't work, as the compiler reports foo * as undefined type. BAH. (and
initialized consts are not yet supported in this compiler).


Paul





---
[ 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: "Kurt Krueckeberg" <kurtk@home.com>
Date: 2000/09/23
Raw View
int is a built-int type(not a struct of class). Use
numeric_limits<int>::max()  // #include <limits> and be sure to do: using
namespace std;

--kurt

"Balog Pal" <pasa@lib.hu> wrote in message
news:000001c02424$2b5afde0$980e38c3@bpnt...
> In msvc5 I tried something like this:
>
> class foo
> {
>  private:
>    int x;
>  public
>    enum {reserve = sizeof(x)};
> };
>
> That had me an error:
> "Compiler Error C2327
> 'symbol' : member from enclosing class is not a type name, static, or an
> enumerator
>
> >From within a nested class, you attempted to access a member of the
> enclosing class that was not a type name, a static member, or an
> enumerator."
>
> Is that the intended behavior? And if yes, why is that good? The example
> coming with an error makes real access to the enclosing class' member,
that
> is obviously an error, as the inner class can't have a 'this' for access.
> But sizeof() does not need anything like that, and could very well work.
>
> What is even more interesting, I tried amember function:
>
> static int GetReserve { return sizeof(x);}
>
> and that produced
> "Compiler Error C2070
> illegal sizeof operand
>
> The operand of a sizeof expression was not an expression or a type name."
>
> Removing static it compiles.
> to make it work as static a nightmare like
>
> static int GetReserve { return sizeof( ((foo*)0)->x);}
>
> is needed. Too bad that is not recognized by the compiler as a 'constant
> expression', highly limiting the use. And trying the same trick on enum
> doesn't work, as the compiler reports foo * as undefined type. BAH. (and
> initialized consts are not yet supported in this compiler).
>
>
> Paul
>
>
>
>
>
> ---
> [ 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              ]