|
Prev: Special container
Next: map within map
From: Palik Imre on 11 Aug 2005 08:51 "B. Perlman" <batya(a)excalibur.co.il> writes: > > #define SOME_CONSTANT 5 // int const some_constant = 5; > I have often seen your version recommended, but I am not completely > convinced. First of all, in an embedded environment, one may be > limited in space such that one is literally counting bytes (yes, I > know, in some cases the place for const variables is different and they > don't take up needed space). I'd eager to see an example where an equivalen static const costs more memory than a #define. Counting the bytes here I couldn't find any. > Also, a more general issue -- if one is using this in more than one > source file, one will want to put it into a header file. It seems to > me that either one must declare it "static" and it will be instantiated > once for each source file in which the header is included, If you #define the thing, it will also be instantiated in every object file, if such an instantiation is needed. E.g. if you #define SOMETHING 0x1ff and then use this SOMETHING, after compiling the file for an ARM core, you will see the allocated space in the generated object file. ImRe [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin Bonner on 11 Aug 2005 08:54 Jeff Schwab wrote: > tony_in_da_uk(a)yahoo.co.uk wrote: > > > Having overhauled thousands of lines of code in the last couple days, > > I'm wondering what other C++ programmer's pet peeves are. I think > > there's some small value in this, as hopefully some of us will see > > things we do and stop, or have someone explain a pet peeve and it won't > > bother us in future. > > // Redundant tests. > > if( some_bool == true ) // if( some_bool ) > if( some_pointer != 0 ) // if( some_pointer ) Interesting. The first I strongly agree with. I get really het-up in code reviews when I see it (I have seen code with bugs because one function was returning -1, and another was testing against TRUE which was defined as 1). On the other hand I intensly dislike the second. In my view 'if' takes a boolean expression, and I have never liked using anything other than a boolean value/expression there. The automatic conversion from integers and pointers just feels wrong (I regret that Dennis Richie omitted "bool" from C in the first place). > T* p = NULL; // T* p = 0; This is another one I strongly disagree with. The fact that the null pointer constant is a literal zero [1] strikes me as another mistake. It *should* have been some magic token. Using NULL allows me to convey pointerishness to my readers (if not the compiler - and yes, I know about possible unfortunate overloads). [1] for the pedants (who me?) "A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero." [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: pookiebearbottom on 11 Aug 2005 13:03 How about std::string s1; std::string s2; if(strcmp(s1.c_str(),s2.c_str())!=0) {} or void IHaveASimpleCFunction() { // do what needs to be done } so to make it C++, they feel like that have to: class IHaveASimpleCFunction { public: IHaveASimpleCFunction() {} // do nothing void operator()() { // do what needs to be done } }; then Just do int main() { IHaveASimpleCFunction i; i(); return 0; } [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jeff Schwab on 11 Aug 2005 13:06 B. Perlman wrote: >>if( some_pointer != 0 ) // if( some_pointer ) > > What's wrong with this? It is clearer than your version, which looks > like a boolean test. I mean, you are not *really* asking if > some_pointer is true... Conditions in C++ don't test for truth, they test for non-zero. >>T* p = NULL; // T* p = 0; > > What's wrong with this? Shows intent more clearly. No it doesn't, it's just more verbose. And you'd better hope some new hire or malicious project member doesn't ever redefine NULL, because this would be one heck of a nasty bug to track. >>#define SOME_CONSTANT 5 // int const some_constant = 5; > > I have often seen your version recommended, but I am not completely > convinced. First of all, in an embedded environment, one may be > limited in space such that one is literally counting bytes (yes, I > know, in some cases the place for const variables is different and they > don't take up needed space). That doesn't take up any extra bytes unless the constant'ss address is taken somewhere in the program, e.g. &some_constant. The constant gets optimized away at compile time. This used to be a difference from C, but I'm not sure about newer C standards. > Also, a more general issue -- if one is using this in more than one > source file, one will want to put it into a header file. It seems to > me that either one must declare it "static" and it will be instantiated > once for each source file in which the header is included, or one must > declare it "extern" and have the "real" declaration in some source > file. It's declared static, but that doesn't mean it's "instantiated" in the sense of having an address at run time. You're just assigning a value to a name the compiler can see, rather than one it can't see. And if by "real" declaration you mean "definition," no, there is none needed for static integer constants, since they need no run-time storage. This feature is specifically intended to help avoid the need for preprocessor constants. > Is there a simpler way, or is it really worth it to do this? In some > cases, the gain (type checking) I find not very helpful Hey, code in the way that makes you happy. :) [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: chris jefferson on 11 Aug 2005 13:03
Jeff Schwab wrote: > #define SOME_CONSTANT 5 // int const some_constant = 5; On one hand this annoys me, on the other hand I've seen some people use it because it avoids adding an extra symbol into your program. Also it prevents people trying to do things like take a pointer to the constant. Many implementations now seem to declare their constants by enumerations. For a while that REALLY confused me, and it still feels like a hack. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |