[SciPy-Dev] bool wrapper in sparse

Nathaniel Smith njs at pobox.com
Sun Jun 30 10:37:07 EDT 2013


On 30 Jun 2013 15:14, "Charles R Harris" <charlesr.harris at gmail.com> wrote:
>
>
>
> On Thu, Jun 27, 2013 at 4:19 PM, Nathaniel Smith <njs at pobox.com> wrote:
>>
>> On 27 Jun 2013 22:56, "Pauli Virtanen" <pav at iki.fi> wrote:
>> >
>> > 27.06.2013 00:22, Blake Griffith kirjoitti:
>> > > So basically I define a class that behaves like bool, but add
compiler
>> > > specific parameters to make instance of this class 1 byte?
>> >
>> > Pretty much so, for known compilers (gcc, msvc).
>> >
>> > I'd also put an assertion somewhere in the code. Either inside one of
>> > the functions, or try to construct a compile-time assertion via some
>> > trickery. C++11 has static_assert that would do the job, but that's a
>> > bit too new standard to rely on. However, there are some tricks to do
it
>> > in any case (check stackoverflow for "sizeof compile time assert").
>>
>> Also it's unlikely that any trickery will be needed on any real world
compiler. Standard c++ guarantees you can cast between npy_bool* and
npy_bool_wrapper*; the only concern is that arrays might not have the same
memory layout. And I don't think any compiler will add padding to a 1 byte
struct. chars don't need word alignment! (But of course I could be wrong.)
>
> IIRC, in ancient times, the Borland compiler added an identifying number
as the first entry in all classes. Needless to say, that played hob with
classes like complex. That innovation didn't last long.

Not only is that a terrible idea, it is (these days) actually a violation
of the standard. But the standard does allow padding *after* the sole
element in a single-element struct.

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20130630/2345390f/attachment.html>


More information about the SciPy-Dev mailing list