Board index » cppbuilder » #if and sizeof()
Andrue Cope [TeamB]
![]() CBuilder Developer |
Andrue Cope [TeamB]
![]() CBuilder Developer |
#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 |
Hendrik Schober
![]() CBuilder Developer |
2005-04-27 05:56:51 PM
Re:#if and sizeof()
Andrue Cope [TeamB] < XXXX@XXXXX.COM >wrote:
QuoteI didn't realise that the C++ standard doesn't allow 'sizeof()' in the 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 |
Andrue Cope [TeamB]
![]() CBuilder Developer |
2005-04-27 06:34:27 PM
Re:#if and sizeof()
Hendrik Schober wrote:
Quote'sizeof' is a C++ language construct, while '#if' Quote(What are you trying to achieve? There's a good 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} |
Hendrik Schober
![]() CBuilder Developer |
2005-04-27 07:20:25 PM
Re:#if and sizeof()
Andrue Cope [TeamB] < XXXX@XXXXX.COM >wrote:
QuoteHendrik Schober wrote: Quote[...] 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 |
Andrue Cope [TeamB]
![]() CBuilder Developer |
2005-04-27 07:30:54 PM
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 |
Alisdair Meredith[TeamB]
![]() CBuilder Developer |
2005-04-28 03:11:49 AM
Re:#if and sizeof()
Andrue Cope [TeamB] wrote:
QuoteWell yes but it seems kind of petty for the C++ standard to forbid it. 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) |
Chris Uzdavinis
![]() CBuilder Developer |
2005-04-28 04:12:46 AM
Re:#if and sizeof()
"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >writes:
QuoteI didn't realise that the C++ standard doesn't allow 'sizeof()' in the I'm not sure if it handles everything or just certain types. -- Chris (TeamB); |
Hendrik Schober
![]() CBuilder Developer |
2005-04-28 05:31:18 PM
Re:#if and sizeof()
Alisdair Meredith[TeamB] <alisdair.meredith@ XXXX@XXXXX.COM >wrote:
Quote[...] 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). QuoteAlisdairM(TeamB) 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 |
Alisdair Meredith [TeamB]
![]() CBuilder Developer |
2005-04-28 05:50:39 PM
Re:#if and sizeof()
Hendrik Schober wrote:
QuoteMhmm. The only thing Google found me is 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) |
Ed Mulroy [TeamB]
![]() CBuilder Developer |
2005-04-28 06:11:34 PM
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 QuoteAndrue Cope wrote in message |
Hendrik Schober
![]() CBuilder Developer |
2005-04-28 11:13:52 PM
Re:#if and sizeof()
Alisdair Meredith [TeamB] < XXXX@XXXXX.COM >wrote:
QuoteHendrik Schober wrote: QuoteI tried the following Google searches as a reasonable starting point - Quote['Order' is Vasa Karvonen's new preprocessor language, Chaos is Paul QuoteBoth seem to be build on the C99 preprocessor, rather than C++. 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 |
Hendrik Schober
![]() CBuilder Developer |
2005-04-28 11:15:49 PM
Re:#if and sizeof()
Ed Mulroy [TeamB] < XXXX@XXXXX.COM >wrote:
Quote[...] on that: 'sizeof', AFAIK, is an operator, not a function. But otherwise you're totally right and I agree with everything else! :-))) Quote. Ed 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 |
Alisdair Meredith [TeamB]
![]() CBuilder Developer |
2005-04-28 11:53:44 PM
Re:#if and sizeof()
Hendrik Schober wrote:
QuoteEd, I am not really disagreeing with you 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) |
Rudy Velthuis [TeamB]
![]() CBuilder Developer |
2005-04-29 01:00:37 AM
Re:#if and sizeof()
At 17:53:44, 28.04.2005, Alisdair Meredith [TeamB] wrote:
QuoteHendrik Schober wrote: 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. |