hitz@sim5.csi.uottawa.ca (Martin Hitz) (04/06/91)
In article <657@taumet.com> steve@taumet.com (Stephen Clamage) writes: >hitz@sim5.csi.uottawa.ca (Martin Hitz) writes: > >>However, I *do* use a define in such a case to avoid declaration of >> N inline functions for N arrays: > >>#define DIM(array) (sizeof(array)/sizeof(*array)) > >ARRRRGGGGHHH! Use inline member or non-member functions to do this. >They will be evaluated at compile time by any decent compiler, and >it avoids the nasty problems of mismatched types and unscoped names >endemic to macros. I do understand most ARRRRGGGGHHHuments in favor of inline functions, however, an inlined DIM function couldn't be used where compile-time expressions are needed (at least not with g++ and Zortech), as in int x[DIM(y)]; That's why I still use macros (from time to time). Martin Hitz (hitz@csi.UOttawa.CA)
steve@taumet.com (Stephen Clamage) (04/07/91)
>In article <657@taumet.com> steve@taumet.com (Stephen Clamage) writes: >>hitz@sim5.csi.uottawa.ca (Martin Hitz) writes: >> >>>However, I *do* use a define in such a case to avoid declaration of >>> N inline functions for N arrays: >> >>>#define DIM(array) (sizeof(array)/sizeof(*array)) >> >>ARRRRGGGGHHH! Use inline member or non-member functions to do this. >>They will be evaluated at compile time by any decent compiler, and >>it avoids the nasty problems of mismatched types and unscoped names >>endemic to macros. Sigh. I guess I didn't explain myself well enough. I didn't mean that the macro DIM could be replaced by an inline function. It can't. I meant that we should be looking for C++ solutions to our programming problems rather than rehashing old C tricks. Macros have lots of problems (I pointed out two with the DIM macro), and IMHO are almost never needed. The DIM macro can only work when the array is declared with a compile-time constant. So why not give that constant a name? const int x_size = 10; T x[x_size]; // T is some type const int y_size = x_size + 2; U y[y_size]; // U is some type For classes containing dynamic arrays, it makes sense to have an inline member function returning data about its size. Anyhow, that's my ARRRRGGGGHHHument (thanks, Martin, for that locution). -- Steve Clamage, TauMetric Corp, steve@taumet.com
weikart@prl.dec.com (Chris Weikart) (04/10/91)
In article <660@taumet.com>, steve@taumet.com (Stephen Clamage) writes: > >In article <657@taumet.com> steve@taumet.com (Stephen Clamage) writes: > >>hitz@sim5.csi.uottawa.ca (Martin Hitz) writes: > >> > >>>However, I *do* use a define in such a case to avoid declaration of > >>> N inline functions for N arrays: > >> > >>>#define DIM(array) (sizeof(array)/sizeof(*array)) > >> > >>ARRRRGGGGHHH! Use inline member or non-member functions to do this. > >>They will be evaluated at compile time by any decent compiler, and > >>it avoids the nasty problems of mismatched types and unscoped names > >>endemic to macros. > > Sigh. I guess I didn't explain myself well enough. I didn't mean that > the macro DIM could be replaced by an inline function. It can't. I > meant that we should be looking for C++ solutions to our programming > problems rather than rehashing old C tricks. Macros have lots of > problems (I pointed out two with the DIM macro), and IMHO are almost > never needed. > > The DIM macro can only work when the array is declared with a compile-time > constant. So why not give that constant a name? > const int x_size = 10; > T x[x_size]; // T is some type > const int y_size = x_size + 2; > U y[y_size]; // U is some type > For classes containing dynamic arrays, it makes sense to have an inline > member function returning data about its size. > > Anyhow, that's my ARRRRGGGGHHHument (thanks, Martin, for that locution). > -- > > Steve Clamage, TauMetric Corp, steve@taumet.com char *blah_blah[] = { "When you can use a constant, use it. When you can't, like here,", "then do what DIM does, with or without DIM. Since it only works", "on genuine arrays (i.e. declared as such, buffers, not pointers),", "this seems to be a good general strategy." } unsigned int blah_blah_num = DIM(blah_blah); ------------------------------------------------------------------------------- Chris Weikart <weikart@prl.dec.com> .............. DEC Paris Research Lab (PRL)
marc@dumbcat.sf.ca.us (Marco S Hyman) (04/10/91)
In article <1991Apr9.175539.29221@prl.dec.com> weikart@prl.dec.com (Chris Weikart) writes: > char *blah_blah[] = > { > "When you can use a constant, use it. When you can't, like here,", > "then do what DIM does, with or without DIM. Since it only works", > "on genuine arrays (i.e. declared as such, buffers, not pointers),", > "this seems to be a good general strategy." > } > > unsigned int blah_blah_num = DIM(blah_blah); But a macro was not needed for the original question. An enum can be used -- if you don't mind using an enum where it doesn't (conceptually) belong. The following compiles without warning using a cfront 2.0 based compiler. class jive { public: void tweak() {} }; class array_of_jive { public: enum jive_constants { num_elements = 10 }; jive a[num_elements]; void tweak_all() { for ( int i = 0 ; i < num_elements; ++i ) a[i].tweak(); } }; -- // marc // home: marc@dumbcat.sf.ca.us pacbell!dumbcat!marc // work: marc@ascend.com uunet!aria!marc