Board index » cppbuilder » #if and sizeof()

#if and sizeof()


2005-04-27 05:52:27 PM
cppbuilder34
I didn't realise that the C++ standard doesn't allow 'sizeof()' in the
constant-expression of a #if.
Does anyone know the reason for this?
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 
 

Re:#if and sizeof()

Andrue Cope [TeamB] < XXXX@XXXXX.COM >wrote:
Quote
I didn't realise that the C++ standard doesn't allow 'sizeof()' in the
constant-expression of a #if.

Does anyone know the reason for this?
'sizeof' is a C++ language construct, while '#if'
is something a very dumb textprocessor processes,
that doesn't know about C++.
(What are you trying to achieve? There's a good
chance you can do this with some simple template
specializations.)
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 

Re:#if and sizeof()

Hendrik Schober wrote:
Quote
'sizeof' is a C++ language construct, while '#if'
is something a very dumb textprocessor processes,
that doesn't know about C++.
Well yes but it seems kind of petty for the C++ standard to forbid it.
Quote
(What are you trying to achieve? There's a good
chance you can do this with some simple template
specializations.)
I'm trying to raise an error if structure packing is not set to byte
alignment. FWIW I'm only experimenting with porting a large code base
to VS.NET so getting this working isn't crucial but it'd be nice of
course to know the solution.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

{smallsort}

Re:#if and sizeof()

Andrue Cope [TeamB] < XXXX@XXXXX.COM >wrote:
Quote
Hendrik Schober wrote:

>'sizeof' is a C++ language construct, while '#if'
>is something a very dumb textprocessor processes,
>that doesn't know about C++.

Well yes but it seems kind of petty for the C++ standard to forbid it.
If it didn't, all preprocessors would need
to understand C++.
Quote
[...]
I'm trying to raise an error if structure packing is not set to byte
alignment. FWIW I'm only experimenting with porting a large code base
to VS.NET so getting this working isn't crucial but it'd be nice of
course to know the solution.
If you don't want to use boost's static
(i.e. compile-time) assertions, then you
can cook up your own for this special
case rather easily:
//Warning: Uncompiled code ahead!
template< std::size_t N_Is, std::size_t N_Should>
struct check_size_helper; // note: undefined
template< std::size_t N>
struct check_size_helper<N,N>{ enum { is_ok = true }; };
template< typename T, std::size_t N>
struct check_size {
enum { intentional_compile_time_error
= check_size_helper<sizeof(T),N>::is_ok };
};
// use it like this:
check_size<X,42>size_checker;
HTH,
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 

Re:#if and sizeof()

Thanks for the comments. Once I've got this project building (getting
closer every minute!) I'll add that in.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:#if and sizeof()

Andrue Cope [TeamB] wrote:
Quote
Well yes but it seems kind of petty for the C++ standard to forbid it.
Not really, as the preprocessor is little but a text-replacing engine.
What is the boolen value of a piece of text?
The sizeof operation is only meaningful at a later stage, when the
compiler has already worked out all the information about how to lay a
struct out in memory.
This is another reason to prefer templates and inline functions over
macros. More of the language is available to help you too.
And if you think template metaprogramming is a black art, just take a
look into the Boost preprocessor library...
[and the Order and Chaos PreProcessor metalanguages if you feel
*really* brave!]
AlisdairM(TeamB)
 

Re:#if and sizeof()

"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >writes:
Quote
I didn't realise that the C++ standard doesn't allow 'sizeof()' in the
constant-expression of a #if.

Does anyone know the reason for this?
Sizeof is calculated by the compiler, not the preprocessor. It is a
neat feature that Borland added to their preprocess, however, though
I'm not sure if it handles everything or just certain types.
--
Chris (TeamB);
 

Re:#if and sizeof()

Alisdair Meredith[TeamB] <alisdair.meredith@ XXXX@XXXXX.COM >wrote:
Quote
[...]
[and the Order and Chaos PreProcessor metalanguages if you feel
*really* brave!]
Mhmm. The only thing Google found me is
lists.boost.org/MailArchives/boost/msg46461.php
which isn't really telling me much. So
what is "Order and Chaos" (besides being
two states on my desktop, one of which
is feared and the other never achieved).
Quote
AlisdairM(TeamB)
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 

Re:#if and sizeof()

Hendrik Schober wrote:
Quote
Mhmm. The only thing Google found me is
lists.boost.org/MailArchives/boost/msg46461.php
which isn't really telling me much. So
what is "Order and Chaos" (besides being
two states on my desktop, one of which
is feared and the other never achieved).
Sorry, I guess I could have gone looking for references myself
<sheepish grin>
I tried the following Google searches as a reasonable starting point -
doesn't look to be much outside newsgroup discussion at the moment
after all:
['Order' is Vasa Karvonen's new preprocessor language, Chaos is Paul
Mensonides. These two people are largely responsible for Boost
PreProcessor language]
www.google.co.uk/search
karvonen&hl=en&lr=&filter=0
and
www.google.co.uk/search
s&meta=
Both seem to be build on the C99 preprocessor, rather than C++.
AlisdairM(TeamB)
 

Re:#if and sizeof()

From long ago, back when what passed for a standard was the K&R book
on C and before C++ was even a gleam in Bjorne's eye, this issue of
sizeof in preprocessor statements had already become a common
question.
The preprocessor phase does text removal and/or substitution. The
result is passed to the compile phase. This same relationship
continues to this day in both C and C++.
While sizeof() is a built in (some would say 'intrinsic') function, it
is still a function and is implemented in the compile phase. During
the preprocessing phase 'sizeof(name)' is still only a set of
characters. At that point it is not a function so there is no return
value to use in the expression.
. Ed
Quote
Andrue Cope wrote in message
news:426f60b9$ XXXX@XXXXX.COM ...

I didn't realise that the C++ standard doesn't allow 'sizeof()'
in the constant-expression of a #if.

Does anyone know the reason for this?
 

Re:#if and sizeof()

Alisdair Meredith [TeamB] < XXXX@XXXXX.COM >wrote:
Quote
Hendrik Schober wrote:

>Mhmm. The only thing Google found me is
>lists.boost.org/MailArchives/boost/msg46461.php
>which isn't really telling me much. So
>what is "Order and Chaos" (besides being
>two states on my desktop, one of which
>is feared and the other never achieved).

Sorry, I guess I could have gone looking for references myself
<sheepish grin>
Not sure what you're getting at.
Quote
I tried the following Google searches as a reasonable starting point -
doesn't look to be much outside newsgroup discussion at the moment
after all:
Ah, I didn't try to check the groups... Sorry.
Quote
['Order' is Vasa Karvonen's new preprocessor language, Chaos is Paul
Mensonides. These two people are largely responsible for Boost
PreProcessor language]

www.google.co.uk/search&lr=&filter=0

and

www.google.co.uk/search&meta=
Ouch. Count me out. Those two do too weird stuff
for my taste. :)
Quote
Both seem to be build on the C99 preprocessor, rather than C++.

AlisdairM(TeamB)
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 

Re:#if and sizeof()

Ed Mulroy [TeamB] < XXXX@XXXXX.COM >wrote:
Quote
[...]
While sizeof() is a built in (some would say 'intrinsic') function, it
is still a function [...]
Ed, I am not really disagreeing with you
(No! Really Not!), but I need to nitpick
on that: 'sizeof', AFAIK, is an operator,
not a function.
But otherwise you're totally right and I
agree with everything else! :-)))
Quote
. Ed
[...]
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 

Re:#if and sizeof()

Hendrik Schober wrote:
Quote
Ed, I am not really disagreeing with you
(No! Really Not!), but I need to nitpick
on that: 'sizeof', AFAIK, is an operator,
not a function.
Yup, 5.3.3
The sizeof operator yields the number of bytes in the object
representation of its operand.
Oddly enough, it appears in neither the list of operators that can be
overloaded, nor the list that cannot. I think I see a defect report...
(also, typeid appears in neither list)
AlisdairM(TeamB)
 

Re:#if and sizeof()

At 17:53:44, 28.04.2005, Alisdair Meredith [TeamB] wrote:
Quote
Hendrik Schober wrote:

>Ed, I am not really disagreeing with you
>(No! Really Not!), but I need to nitpick
>on that: 'sizeof', AFAIK, is an operator,
>not a function.

Yup, 5.3.3
The sizeof operator yields the number of bytes in the object
representation of its operand.


Oddly enough, it appears in neither the list of operators that can be
overloaded, nor the list that cannot. I think I see a defect report...

(also, typeid appears in neither list)

AlisdairM(TeamB)
I guess it would become extremely tricky if it were overloadable. But
AFAIK, what distinguishes it from most other operators is that it is a
compile-time-only operator.
--
Rudy Velthuis [TeamB]
"I hear Glenn Hoddle has found God. That must have been one hell
of a pass." -- Bob Davies.