10

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.

58 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

22
#define private public
18
if (!this) return;
6

On a project at work I see people go:

if(false){

//Old code here

}

instead of using:

/*

*/

or just using cvs, like normal people.

5

;

5

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

4

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.

3

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();
}
3

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.

2

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

2

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).

2

alt text

delete this;

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

1

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
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.

-1
#include <stdio.h>
-2

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!!!"