share
Stack OverflowThe most abusive C++ you've ever seen used?
[+10] [16] fbrereto
[2009-08-27 03:19:27]
[ c++ polls discussion fun ]
[ http://stackoverflow.com/questions/1338683] [DELETED]

I'm curious to know, what is the most abrasive, caustic, abusive but perfectly legal and correct C++ you've ever seen ship out the door?

This question isn't meant to be a C++ bashing session, but a humorous introspection into the world of C++ we allow ourselves (or others) to get away with.

Poll question = should be community wiki - gnovice
(1) Change it to community wiki, this isn't live journal. - Sergio Tapia
(4) "Insipid" means lacking in taste; stuff which is abrasive, caustic and abusive is unlikely to be insipid. - Jonathan Leffler
(1) Not sure how illegal C++ would get shipped out the door! - Alex
(3) closed? So sad. - beggs
(4) Why is this question closed? I just don't understand I should be refrained from posting an answer. Oh well... - Dimitri C.
(1) I second this. It is relevant and intersting question. - ttvd
(1) Why is this question closed? C++ is a language with a lot of abusing potential, let's not waste it!!! - Eduardo León
(1) "relevant"? To who is this relevant, and how, exactly? I'm having a hard time seeing it as anything more than a way to goof off and brag about how bad code you've seen in your career. Like those 300 other questions asking infinite variations of the same. It'd be nice if people were at least honest when they voted for reopening. Instead of pretending it is "relevant", say that it's fun. Or that it lets you vent about the crappy code you once saw a coworker write. Or that you get a kick out of popularity contests like these. Whatever. But it's hardly "relevant". - jalf
(1) "Relevant" is very subjective. Just like most of SO it seems. - ttvd
[+58] [2009-08-27 06:57:26] ttvd [ACCEPTED]

The most abusive C++ I've seen by far is "Analog Literals" by Eelis.

It basically lets you do this:

  unsigned int c = ( o-----o
                     |     !
                     !     !
                     !     !
                     o-----o ).area;

or

   unsigned int c = ( o-------------o
                      |L             \
                      | L             \
                      |  L             \
                      |   o-------------o
                      |   !             !
                      !   !             !
                      o   |             !
                       L  |             !
                        L |             !
                         L|             !
                          o-------------o ).volume;

See here http://www.xs4all.nl/~weegen/eelis/analogliterals.xhtml


(2) Incredible!!!!!!!!!!!!!! - Miguel Angel
I have no words... that's some crazy stuff. - fbrereto
"Good... Use your aggressive feelings, boy!" This feels just like the dark side of the Force - fishlips
(1) that is insane! - mezoid
No words...should have sent a poet! - Mike Clark
shiitt... that's an ugly hack! - Atmocreations
The purpose of this library contradicts itself: "Have you ever felt that integer literals like "4" don't convey the true size of the value they denote? If so, use an analog integer literal instead ... use N*2+1 dashes between the I's to get a value of N." - Steve Guidi
(1) @Steve you may wanna read Tweaking Analog Literals then :) - Johannes Schaub - litb
1
[+22] [2009-08-27 07:07:22] h0b0
#define private public

(8) Don't read this one while drinking coffee, or you'll be cleaning your monitor. - fbrereto
(11) I like: #define if while better - windfinder
(2) I had to do this when working with MFC because some function that was obviously meant to be public was actually private due to MFC developers not understanding complications of some MFC macro that changed the visibility. - erikkallen
(1) I once put #define struct union in someone's c code to see how long it would take them to find it. Quite a while... - JohnB
2
[+18] [2009-08-27 03:28:46] 280Z28
if (!this) return;

I don't get it, feels dumb, this type of statement is quite typical in Objective-C, if (!self) return nil;. - dreamlax
Sadly I've seen plenty of "assert(NULL != this);" - akent
Regarding the differences between this and self: stackoverflow.com/questions/1341734/… - fbrereto
(1) I'm probably missing something here but what is wrong with asserting that this != NULL? - JohnB
@JB: Nothing, IMHO. Useful for catching null pointer dereferences at runtime. - JBRWilkinson
3
[+6] [2009-08-27 03:41:47] windfinder

On a project at work I see people go:

if(false){

//Old code here

}

instead of using:

/*

*/

or just using cvs, like normal people.


Even worse is while (false) ... - Kristopher Johnson
(11) /* */ can be interrupted by other comments. I'll occasionally use a #ifdef 0, but if(false) is safer still. Also, you can set the PC into the block with a debugger if you want to test the old behaviour. - Dan O
(11) the if(false) may actually be useful to ensure it will still compile when "commented-in" again when you make changes elsewhere. - Johannes Schaub - litb
(34) Normal people use CVS? - avakar
(2) I often use this for temporarily disabling code. This way, it is still compiled, and you make clear the code is not obsolete. - Dimitri C.
Normal people extend the language with <* ... *> comments (or whatever symbols you like), like in PeopleCode. It doesn't take a genius to write a program that reads a file and outputs a similar file but without these comments. - Eduardo León
(2) Putting the PC in the commented-out block during debugging sounds like a scary tactic; who's to say the compiler doesn't just optimize it out? Just curious... - fbrereto
(1) @avakar: normal people use SVN these days. Or GIT ;) - samuil
I also do that sometimes. Then if I'm debugging, I can step up to that point, move the PC down 1 line, and do the code inside if I want to. - Mike Dunlavey
@fbrereton: I get really annoyed by compilers that get that aggressive about optimizing. If I write the source, I want it compiled. I don't want it to do any more optimization than I would if I were writing the assembler code. That's the kind of thing that nanny-languages do, and it totally misses the point of what optimization is about. REAL optimization is my job, not the compiler's. - Mike Dunlavey
(3) @Mike Dunlavey: You can't optimize as well as the compiler can (unless you do write the assembler code yourself, and are really good at it), and you wouldn't want to have to do it, anyway. Stick to efficient algorithms and no gratuitous inefficiencies, and let the compiler do the rest. It helps if the compiler has a debug mode, but I want the compiler to do all the optimization it can in some mode. - David Thornley
@febrerto: In a debug build, there's no optimisation on by default. - JBRWilkinson
4
[+5] [2009-08-27 03:20:13] OscarRyz

;


for a second there I thought you hadn't written anything... - mezoid
(1) Huh? Just a semicolon? What? Why? - Rasmus Kaj
It's valid C++, isn't it? - Phoshi
It isn't a whole program, but we're posting snippits anyway. That's a valid C++ statement. It's sort of useful sometimes, but easy to misread. - David Thornley
Many people prefer {} when writing e.g. an empty loop, but how is this abusive? - Mechanical snail
5
[+5] [2009-08-27 07:07:20] beggs

#include <afxwin.h> //basic MFC include


(1) The MFC might not be elegant, but it serves its purpose. - Eduardo León
6
[+4] [2009-08-27 07:00:51] Totonga

I have seen stuff like this, tricking around with const :-)

class abc {
public:
    long GetVal() const
    {
        return const_cast<abc*>(this)->GetVal();
    }

    long GetVal()
    {
        return member_++;
    }
private:
    member_;
};

Apparently the author hadn't heard of mutable.


Implementing the const version in terms of the non-const similar to that is actually a fine thing to do -- the real problem here is the existence of a const version of a logically-mutating method. - me22
(1) Most time not. Its just that they do not know about mutable. Its more GetVal is a const method most time. member_ could be an access counter or whatever variable. So mutable member_ woud allow you to make the compiler check wheather none other members are changed. - Totonga
7
[+3] [2009-08-27 06:16:08] Igor Zevaka

Calling non-virtual functions that do not modify member variables on null pointers like so:

class CObject 
{
public:
void CallMeOnNull()
{
  int i = 0;
  i++;
}

virtual void DontCallMeOnNull()
{
  int i = 0;
  i++;
}

void DontCallMeOnNull2()
{
  m_iTest++;
}
private:
    int m_iTest;
};

int test()
{
  CObject* pObject = NULL;
  pObject->CallMeOnNull(); //IS OK???!!!
  pObject->DontCallMeOnNull();
  pObject->DontCallMeOnNull2();
}

8
[+3] [2009-09-03 19:28:56] Mike Dunlavey

Here's some downvote-bait. I do a couple things that reduce errors in editing, but look funny, like

MyFunction(1stArg
    , 2ndArg
    , 3rdArg
    ... etc. ...
    , lastArg
    );

because if I want to add a new argument to the function, I often add it at the end, so it is a 1-line edit, rather than 2 lines.

Another example:

if (false){ }
else if (Test1){
    ... do stuff ...
}
else if (Test2){
    ... do stuff ...
}
... more tests ...
else {
    ... do stuff ...
}

Why? because it makes it easier to insert, delete, or re-order the tests without the first one having different syntax (and thus edited differently) from the rest. This is especially useful in generated code.


What's the point in the first one? That the comma is at the start of the line? - jalf
@jalf: Right, so if you want to insert an additional argument, it's just a 1-line insertion, rather than inserting "<comma><newline & tabs> <new last arg>" in front of parenthesis on old last line. - Mike Dunlavey
+1 @Mike: not too dumb. i'll remember that one the next time i'm building complex if's :D thx for the idea - Atmocreations
9
[+2] [2009-11-10 20:43:37] kriss

I occurred to work on a program where the previous programmer systematically checked that address of non dynamic buffers where not NULL... at first it just look dumb, but when applied to a full program it makes it quite harder to read and maintain.

char buf[10];
if (buf != NULL) {
...
}

That was C, but you can do the same with C++ (hopefully not).


10
[+2] [2009-12-12 13:21:19] Jeff Foster

alt text

delete this;

It's never nice to see suicidal objects, but sometimes it makes sense. The FAQ entry [1] explains why it sometimes makes sense.

[1] http://www.faqs.org/faqs/C++-faq/part6/

(1) Makes sense for reference counting. - Cecil Has a Name
+1 for "suicidal objects". - Mechanical snail
11
[+2] [2009-08-27 03:36:15] Eduardo León

I once did templates the C (not C++) way: using #includes, #defines, #ifdefs and recursion at compilation time.


12
[+1] [2009-12-12 13:03:09] Igor Zevaka

Got another one. Calling a constructor on a class explicitly creates and destroys an object straight away:

class CClass
{
public:
    CClass()
    {
    	printf("I am teh constructor");
    }
    ~CClass()
    {
    	printf("I am teh destructor");
    }
};

//...Somewhere in main
printf("Before constructor test");
CClass::CClass();
printf("After constructor test");

What you will see is this:

Before constructor test
I am teh constructor
I am teh destructor
After constructor test

(1) That's just an unnamed temporary variable whose scope is restricted to a single statement. - mskfisher
13
[+1] [2009-12-12 13:15:36] Cecil Has a Name
SDLSurface *surf = "C:\\images\\picture1.jpeg";

I was helping someone get started with SDL, when I noticed that data tips in the Visual C++ debugger showed an actual file path when the mouse pointer was hovered over an SDLSurface pointer, which lead me to discover this piece of code. When I asked him why, he said he didn't know what to do, so he had to just try something. He's in the 3rd year of a CS master.


14
[-1] [2009-08-27 06:20:59] Alex
#include <stdio.h>

Care to explain why this is abusive? - Graeme Perrow
(2) Well, it should be <cstdio>, but I agree it's not exactly abusive. - me22
15
[-2] [2010-04-26 11:19:46] Ravi Vyas

String batman_dialog = "BOOM POW WHACK KAPOW!!!!!"; String robin_dialog = "Holy caps batman"; String bg_music = "na na na na na na na na na na na na na batman!!!"


16