Topic: Using sizeof with nonstatic member variable


Author: iltchenko@yahoo.com (Andrei Iltchenko)
Date: Tue, 5 Jun 2001 14:40:40 GMT
Raw View
Dietmar Kuehl <dietmar_kuehl@yahoo.com> wrote in message news:<3B17B12A.3EB22B89@yahoo.com>...
> Hi,
> Andrei Iltchenko wrote:
> > Why don't you use something like:
> >
> > struct s { int i; };
> >
> > template<typename T, class Cl>
> > inline size_t  get_size(T Cl::*)
> > {   return  sizeof(T);   }
> >
> > int  main()
> > {
> >    std::cout << get_size(&s::i) << '\n';
> >    return  0;
> > }
> >
> > Though the expression 'get_size(&s::i)' is not a constant expression, with
> > optimizations on MSVC 6.0 SP5 was able to calculate the size at compile
> > time, thus incurring no performance penalty.
>
> You mentioned on possible reason not to use something like this: It is
> not a constant
> expression. However, this can easily be changed:
>
>   template <typename T, typename Cl> T& get_size(T Cl::*); // only
> declared!
>   int main() {
>     std::cout << sizeof(get_size(&s::i)) << '\n';
>   }

Live and learn!
Dietmar, many thanks for this thought.


Kind regards,

Andrei Iltchenko.

---
[ 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: iltchenko@yahoo.com (Andrei Iltchenko)
Date: Tue, 5 Jun 2001 14:41:01 GMT
Raw View
Steve Rencontre <> wrote in message news:<aakehts9tn02tntmaf6os4gcj48aeqm96o@4ax.com>...
> On Thu, 31 May 2001 21:44:47 GMT, "Andrei Iltchenko"
> <iltchenko@yahoo.com> wrote:
>
> >The fact that the operand of the sizeof operator is not evaluated does not
> >mean that the expression can be ill-formed.
>
> Ok, I can sort of reluctantly accept that :-)
>
> >Why don't you use something like:
> >
> >struct s { int i; };
> >
> >template<typename T, class Cl>
> >inline size_t  get_size(T Cl::*)
> >{   return  sizeof(T);   }
>
> First thought: that's uglier than my macro! I know some people have a
> personal vendetta against the preprocessor, but I like it and consider
> it a major strength of the C/C++ language compared to some others.

sizeof ((s *) 0)->i

I think the aesthetic prettiness of a given C++ construct goes after
its semantic soundness.


>
> >Though the expression 'get_size(&s::i)' is not a constant expression
>
> Second thought: I would rather have something that was a genuine
> constant expression.

Given the refinement suggested by Dietmar Kuehl:

#include <iostream>

template<typename T, class C>
inline T  get_size(T C::*);

struct  s  {   char   i;   };

int  main()
{
   std::cout << sizeof get_size(&s::i) << '\n';
   return  0;
}

this is an issue no longer. 'sizeof get_size(&s::i)' in the piece of
code above is a bona fide integral constant expression.


Cheers,

Andrei Iltchenko.

---
[ 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: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 31 May 2001 19:30:41 GMT
Raw View
Steve Rencontre wrote:
>
> Hi group,
>
> I originally posted this in comp.lang.c++ and somebody suggested I ask
> over here.
>
> Anyway, the following code seems pretty obvious and reasonable to me:
>
>         struct s { int i; };
>         const int c = sizeof s::i;
>
> However, it appears that this usage is forbidden by the standard, not
> being one of those explicitly listed in 5.1/10.

3.2p2: "An expression is _potential evaluated_ unless it is the operand
of the sizeof operator ... An object ... is _used_ if its name appears
in a potentially-evaluated expression."

5.1p10: "An _id-expression_ that denotes a nonstatic data member ... of
a class can only be used: ..."

It's inside sizeof(), therefore it's not potentially evaluated,
therefore it's not used, and therefore 5.1p10 doesn't apply.

---
[ 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: "Andrei Iltchenko" <iltchenko@yahoo.com>
Date: Thu, 31 May 2001 21:44:47 GMT
Raw View
<Steve Rencontre> wrote in message
news:lts9ht8hf8hvg449lhpcs18ji9p7iptqjo@4ax.com...
> Hi group,
>
> I originally posted this in comp.lang.c++ and somebody suggested I ask
> over here.
>
> Anyway, the following code seems pretty obvious and reasonable to me:
>
> struct s { int i; };
> const int c = sizeof s::i;
>
> However, it appears that this usage is forbidden by the standard, not
> being one of those explicitly listed in 5.1/10.
>
> Ok, so the standard is the standard, and it says that I can't do that,
> but I can't see any good reason why the construct should not be
> accepted. If anyone knows better, I'd be interested to hear.

Because the construct 'sizeof s::i' matches the syntax of 'sizeof
unary-expression'. As the unary-expression references a non-static data
member of s and not for the purpose of taking its address, the object
expression must be either implied (when the keyword 'this' is in scope) or
stated expicitly (when an explicit member access is used). Otherwise the
member access makes no sense, for what object's member are you accessing?
The fact that the operand of the sizeof operator is not evaluated does not
mean that the expression can be ill-formed.


> AFAICT the value is always well-defined and accessible to the
> compiler, and it's not as if I can't get the effect another way. I'm
> presently using
>
> sizeof ((s *) 0)->i
>
> wrapped up in a macro to disguise its ugliness :-(

Why don't you use something like:

struct s { int i; };

template<typename T, class Cl>
inline size_t  get_size(T Cl::*)
{   return  sizeof(T);   }

int  main()
{
   std::cout << get_size(&s::i) << '\n';
   return  0;
}

Though the expression 'get_size(&s::i)' is not a constant expression, with
optimizations on MSVC 6.0 SP5 was able to calculate the size at compile
time, thus incurring no performance penalty.


Cheers,

Andrei Iltchenko.



---
[ 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: Dietmar Kuehl <dietmar_kuehl@yahoo.com>
Date: Fri, 1 Jun 2001 16:35:26 GMT
Raw View
Hi,
Andrei Iltchenko wrote:
> Why don't you use something like:
>
> struct s { int i; };
>
> template<typename T, class Cl>
> inline size_t  get_size(T Cl::*)
> {   return  sizeof(T);   }
>
> int  main()
> {
>    std::cout << get_size(&s::i) << '\n';
>    return  0;
> }
>
> Though the expression 'get_size(&s::i)' is not a constant expression, with
> optimizations on MSVC 6.0 SP5 was able to calculate the size at compile
> time, thus incurring no performance penalty.

You mentioned on possible reason not to use something like this: It is
not a constant
expression. However, this can easily be changed:

  template <typename T, typename Cl> T& get_size(T Cl::*); // only
declared!
  int main() {
    std::cout << sizeof(get_size(&s::i)) << '\n';
  }

The only use for the function 'get_size()' is to turn the member into
something useful
to 'sizeof()' (I don't know whether '&s::i' is already something useful;
apparently it
is not at least with some compilers).
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.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://www.research.att.com/~austern/csc/faq.html                ]





Author: Steve Rencontre <>
Date: Fri, 1 Jun 2001 19:13:43 GMT
Raw View
On Thu, 31 May 2001 19:30:41 GMT, "James Kuyper Jr."
<kuyper@wizard.net> wrote:

>Steve Rencontre wrote:
>>
>> Hi group,
>>
>> I originally posted this in comp.lang.c++ and somebody suggested I ask
>> over here.
>>
>> Anyway, the following code seems pretty obvious and reasonable to me:
>>
>>         struct s { int i; };
>>         const int c = sizeof s::i;
>>
>> However, it appears that this usage is forbidden by the standard, not
>> being one of those explicitly listed in 5.1/10.
>
>3.2p2: "An expression is _potential evaluated_ unless it is the operand
>of the sizeof operator ... An object ... is _used_ if its name appears
>in a potentially-evaluated expression."
>
>5.1p10: "An _id-expression_ that denotes a nonstatic data member ... of
>a class can only be used: ..."
>
>It's inside sizeof(), therefore it's not potentially evaluated,
>therefore it's not used, and therefore 5.1p10 doesn't apply.

Ah, interesting...

However, I think you're mistaken. It depends on what the word "used"
is used for, as Bill Clinton might say ;-)

The first context is talking about a technical meaning of the word in
relation to an object, but the second is talking about the permissible
usage of an expression.

Pity, really...

--
Steve Rencontre  http://www.rsn-tech.co.uk/
//#include <disclaimer>

---
[ 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: Steve Rencontre <>
Date: Sat, 2 Jun 2001 02:23:31 GMT
Raw View
On Thu, 31 May 2001 21:44:47 GMT, "Andrei Iltchenko"
<iltchenko@yahoo.com> wrote:

>The fact that the operand of the sizeof operator is not evaluated does not
>mean that the expression can be ill-formed.

Ok, I can sort of reluctantly accept that :-)

>Why don't you use something like:
>
>struct s { int i; };
>
>template<typename T, class Cl>
>inline size_t  get_size(T Cl::*)
>{   return  sizeof(T);   }

First thought: that's uglier than my macro! I know some people have a
personal vendetta against the preprocessor, but I like it and consider
it a major strength of the C/C++ language compared to some others.

>Though the expression 'get_size(&s::i)' is not a constant expression

Second thought: I would rather have something that was a genuine
constant expression.

Thanks anyway :-)

--
Steve Rencontre  http://www.rsn-tech.co.uk/
//#include <disclaimer>

---
[ 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: Steve Rencontre <>
Date: Wed, 30 May 2001 15:41:16 GMT
Raw View
Hi group,

I originally posted this in comp.lang.c++ and somebody suggested I ask
over here.

Anyway, the following code seems pretty obvious and reasonable to me:

 struct s { int i; };
 const int c = sizeof s::i;

However, it appears that this usage is forbidden by the standard, not
being one of those explicitly listed in 5.1/10.

Ok, so the standard is the standard, and it says that I can't do that,
but I can't see any good reason why the construct should not be
accepted. If anyone knows better, I'd be interested to hear.

AFAICT the value is always well-defined and accessible to the
compiler, and it's not as if I can't get the effect another way. I'm
presently using

 sizeof ((s *) 0)->i

wrapped up in a macro to disguise its ugliness :-(


--
Steve Rencontre  http://www.rsn-tech.co.uk/
//#include <disclaimer>

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