In page 148 (N4296) there's a static_assert example
-----------------------------
[ Example:
static_assert(sizeof(long) >= 8, "64-bit code generation required for this library.");
—end example ]
-----------------------------
For such a short code, there seems to be two errors, but I would like have a confirmation:
-sizeof(long) should be sizeof(void*)?
-Comparison should be == 8 or the comment "At least 64-bit code generation..."?
While this is just a small example, for someone having frustrating experiences with code that won't work in 64-bit Windows build at least partly due to pointers being casted to long (in 64-bit MSVC sizeof(long) < sizeof(void*)), this kind of example in C++ standard itself stands out especially since, please correct if wrong, the code is not portable C++.
On Sunday, March 15, 2015 at 9:33:25 AM UTC-4, u97...@gmail.com wrote:In page 148 (N4296) there's a static_assert example
-----------------------------
[ Example:
static_assert(sizeof(long) >= 8, "64-bit code generation required for this library.");
—end example ]
-----------------------------
For such a short code, there seems to be two errors, but I would like have a confirmation:
-sizeof(long) should be sizeof(void*)?
Few are the C++ compilers that, when compiling 64-bit, will make long 64-bits in size. However, that doesn't mean that a particular user might not write code that only works on those few compilers. So the test itself is not a priori wrong, simply unconventional.
-Comparison should be == 8 or the comment "At least 64-bit code generation..."?
While this is just a small example, for someone having frustrating experiences with code that won't work in 64-bit Windows build at least partly due to pointers being casted to long (in 64-bit MSVC sizeof(long) < sizeof(void*)), this kind of example in C++ standard itself stands out especially since, please correct if wrong, the code is not portable C++.
Any such static_assert is not intended to be "portable C++". The only way to write completely portable C++ is to work with whatever sizes the compiler supplies. By putting this static_assert in, you're announcing that you aren't writing "portable C++".
On Sunday, 15 March 2015 18:48:25 UTC+2, Nicol Bolas wrote:On Sunday, March 15, 2015 at 9:33:25 AM UTC-4, u97...@gmail.com wrote:In page 148 (N4296) there's a static_assert example
-----------------------------
[ Example:
static_assert(sizeof(long) >= 8, "64-bit code generation required for this library.");
—end example ]
-----------------------------
For such a short code, there seems to be two errors, but I would like have a confirmation:
-sizeof(long) should be sizeof(void*)?
Few are the C++ compilers that, when compiling 64-bit, will make long 64-bits in size. However, that doesn't mean that a particular user might not write code that only works on those few compilers. So the test itself is not a priori wrong, simply unconventional.
[assuming you meant "will make" = "wlll not make"]
As said Microsoft Visual C++ is one of those compilers, so we're not talking about marginal compilers here. But whether there are few or many is irrelevant, this is about correctness. If the intention of the test, as the comment suggests, is to test for 64 build (sizeof(void*) == 8), it's simply wrong, not unconventional.
-Comparison should be == 8 or the comment "At least 64-bit code generation..."?
While this is just a small example, for someone having frustrating experiences with code that won't work in 64-bit Windows build at least partly due to pointers being casted to long (in 64-bit MSVC sizeof(long) < sizeof(void*)), this kind of example in C++ standard itself stands out especially since, please correct if wrong, the code is not portable C++.
Any such static_assert is not intended to be "portable C++". The only way to write completely portable C++ is to work with whatever sizes the compiler supplies. By putting this static_assert in, you're announcing that you aren't writing "portable C++".
By portable in this context I meant that it will do the right thing for all compilers where the 64-bit build can be done.
On Sunday 15 March 2015 06:33:25 u97...@gmail.com wrote:
> [ Example:
> static_assert(sizeof(long) >= 8, "64-bit code generation required for this
> library.");
> —end example ]
Don't worry, that's not how people detect 64-bit builds.
Most code simply checks for the _WIN64 and __LP64__ macros...
On Sunday, 15 March 2015 19:37:06 UTC+2, Thiago Macieira wrote:On Sunday 15 March 2015 06:33:25 u97...@gmail.com wrote:
> [ Example:
> static_assert(sizeof(long) >= 8, "64-bit code generation required for this
> library.");
> —end example ]
Don't worry, that's not how people detect 64-bit builds.
Most code simply checks for the _WIN64 and __LP64__ macros...
It's not about the way to detect 64-bit platform, it's about the promotion of the misconception that it's correct to assume sizeof(long) == sizeof(void*), which I think is what the example is implicitly doing. And the reason for the attention to such detail is not theoretical, but stem from seeing too much code that has no Win64 build due to such problems and when that assumption propagates to KLOC or MLOC codebase, fixing it can be very far from being trivial.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.
Being the one bringing up this matter, I can take a look at submitting an issue, although I might leave the 'proposed wording' empty so that more proficient people can come up with a better example.
static_assert(std::numeric_limits<int>::min() < -std::numeric_limits<int>::max(),
"This library requires a two's-complement machine.");
static_assert((std::numeric_limits<int>::min)() < -(std::numeric_limits<int>::max)(),
"This library requires a two's-complement machine.");
On Monday, March 30, 2015 at 9:33:10 AM UTC-7, u97...@gmail.com wrote:Being the one bringing up this matter, I can take a look at submitting an issue, although I might leave the 'proposed wording' empty so that more proficient people can come up with a better example.
static_assert(std::numeric_limits<int>::min() < -std::numeric_limits<int>::max(),
"This library requires a two's-complement machine.");
I traditionally write this with extra parentheses, though, due to the tendency for some headers to define min and max macros:
static_assert((std::numeric_limits<int>::min)() < -(std::numeric_limits<int>::max)(),
"This library requires a two's-complement machine.");
static_assert(std::numeric_limits<int>::min() < -std::numeric_limits<int>::max(),
"This library requires a two's-complement machine.");
On 2015–03–31, at 12:33 AM, u97...@gmail.com wrote:Being the one bringing up this matter, I can take a look at submitting an issue, although I might leave the 'proposed wording' empty so that more proficient people can come up with a better example.
The whole point of the test is that -max = min + 1 in two's complement systems, whereas -max = min on one's complement and sign-magnitude.