Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!olivea!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: macros (was: const vs. static in class declaration) Keywords: macros, inline Message-ID: <660@taumet.com> Date: 6 Apr 91 17:59:45 GMT References: <3864@island.COM> <1991Apr3.202015.7402@csi.uottawa.ca> <657@taumet.com> <1991Apr5.211511.19618@csi.uottawa.ca> Distribution: na Organization: Taumetric Corporation, San Diego Lines: 33 >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