[haiku-bugs] Re: [Haiku] #10396: [PATCHSET] Make Haiku userland compile and work with Clang

  • From: "stippi" <trac@xxxxxxxxxxxx>
  • Date: Sun, 12 Jan 2014 12:26:49 -0000

#10396: [PATCHSET] Make Haiku userland compile and work with Clang
---------------------------+----------------------------
   Reporter:  js           |      Owner:  bonefish
       Type:  enhancement  |     Status:  reopened
   Priority:  normal       |  Milestone:  R1/beta1
  Component:  - General    |    Version:  R1/Development
 Resolution:               |   Keywords:  clang
 Blocked By:               |   Blocking:
Has a Patch:  1            |   Platform:  All
---------------------------+----------------------------

Comment (by stippi):

 Replying to [comment:18 js]:
 > About the Type* operator(): This basically makes sure that you can use
 it like a normal array, i.e. it gets automatically converted to a pointer,
 just like a normal array. The idea was that you can easily switch code to
 BStackOrHeapArray. The only thing that of course will break is sizeof().
 But sizeof on an array isn't a good idea anyway, as just switching that
 array to a pointer will also break it.

 I understand that much. What wasn't clear to me is how the compiler knew
 what to do in cases such as these:

   BStackOrHeapArray<int32, 64> someInts(arraySize);

   SomeFunctionExpectingSizeTPointer((size_t*)&someInts[arrayIndex]);

 But Ingo told me that the compiler has to try to convert the object to a
 pointer first, before it can access it as an array, and since the Type*
 operator() is the only way to do that, it does indeed do the right thing.
 Still, it can be non-obvious what happens there, at least it has been for
 me. What is or isn't ugly code, is really debatable. For example, I would
 not have considered this ugly code, and this was my original suggestion
 all the time ago:

   BStackOrHeapArray<int32, 64> someIntsArray(arraySize);
   int32* someInts = someIntsArray.Data();

   SomeFunctionExpectingSizeTPointer((size_t*)&someInts[arrayIndex]);

 This is just one more line per declaration, but removes BStackOrHeapArray
 completely from what you need to know about the code that follows. Or in
 another words, if you could understand the code before the change, you can
 still understand it just fine, no extra knowledge needed.

 (I know that one should not cast an int32 to size_t, because it breaks on
 x86_64... there are TODOs in app_server about this.)

--
Ticket URL: <http://dev.haiku-os.org/ticket/10396#comment:21>
Haiku <http://dev.haiku-os.org>
Haiku - the operating system.

Other related posts: