Click here to Skip to main content
Click here to Skip to main content

Tagged as

Amusing C++: funny tricks

, 22 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A few uncommon code sample

Amusing C++

 

Lisa Simpson gave three apples to her brother Bart, then took back one.

Q.: How many apples Bart has got?

A.: Who knows? We're still unaware of how many apples he had got before!

 

Can C++ be funny? Let's check it out! Here are some examples that gladden the programmer's eye. Be welcome to look at them!

 

Let us start - and may the Source be with you! Suppose we have the following code:

 
int a = 5;
int b = a++ + ++a;
 

What does b equal? Are you sure? Smile | :) The point is that the right value lies somewhere between 10 and 14! My GCC returns 12, but that's not the limit yet! The C++ Standard does not presuppose the calculation of the ++ operator before the assignment operator is done - so that the answer may vary depending on the compiler, it may even depend on the optimization keys. Note that the result of the operation i = i++ is not defined either! People who want to learn more, can google "C++ sequence point", while we go on!

 

What do you think - is this code workable?

 
int i[i];
int j = j;
x x;
int y::y = x.x;
 

Strange as it may seem, it is - if we do like this:

 
const int i = 1;
const int j = 2;
struct x
{
   int x;
};
namespace y
{
   int i[i];
   int j = j;
   x x;
   int y::y = x.x;
};
 

Visibility overlap did its dirty deed! Smile | :) However, MSVC does not accept such code: it considers the declaration of int x to be a declaration of the constructor (the latter, as is known, cannot return a value). And, what's even worse, the namespaced variable j will have an unexpected value, though it is "initialized". So, this clearly indicates that such "tricks" are not worth being played!

 

Let's go on with the next example. Here's another nice code I've gotten you into! Smile | :) This one marked the beginning of my collection of C++ pranks, and I like it the most. Apart from being a really cool prank, it also comprises a psychological trick. Just look at this:

 
T t1;
T t2(t1);
T t3 = t1;
T t4();
 

Which constructor will be called in each of these four cases? If you have decided that those must be:

  • default constructor,
  • copy constructor,
  • assignment operator, and
  • default constructor again,
then you are mistaken... twice! Of course, first comes the default constructor; second comes the copy one, which apparently follows from the syntax. But in the third case, in spite of the = sign, ye good olde copy constructor is activated: we create a new object, not initialize an existing one! In the last case, that's not the creation of an object at all, that's... the declaration of a function! The latter does not take the parameters and returns the type T; compare:

 
int func();
 

If you made no mistakes, congratulations! If you made one nevertheless and are now craving another chance to win, here's a new opportunity for you! Suppose you have this class:

 
class C {
public:
    C()                    { printf("default\n"); }
    C(long)                { printf("long\n"); }
    explicit C(int)        { printf("int\n"); }
    C(const C&)            { printf("copy\n"); }
    C &operator=(const C&) { printf("op=\n"); return *this; }
};
 

Also, you have a code that involves it:

 
void prn(int n)
{
   cout << n << ": ";
}
int main()
{
   const int i = 0;
   prn(1); C c1();
   prn(2); C c2 = i;
   prn(3); C c3(i);
   prn(4); C c4 = C(i);
   prn(5); C c5 = (C) i;
   prn(6); C c6 = static_cast<c>(i);
   return 0;
}</c>
 

What will be displayed on the screen then? To be honest, I made a mistake myself here. And that mistake was severe and reiterated Smile | :) So, please be extremely concentrated. Done? The right answer is:

 
1: 2: long
3: int
4: int
5: int
6: int    
 

Naturally, one is blank; as you remember, it is a function declaration. Two did not manage to build a class from int, since the int-constructor is specified as explicit; however, the compiler obligingly converted int into long - where its own constructor already exists. The rest of options are variations on a theme of C(i), although differently decorated. I cannot but admire the competence of the translator, which used neither excessive copy constructor, nor assignment constructor, nor even default constructor. C++ at its best!

 

Here's the last example - maybe the most "practical" among those cited. Suppose you need to create a cycle, in which the value of a variable tends to zero. Certainly, you can use the operator for. But we can also do like this!

 
int i = 10;
while (i --> 0)
{
   cout << i << endl;
}
 

This thingamajig is called "arrow operator", just in case.

 

Here were the tricks originating in our more-than-native language. And then, C++0x may well come, which conceals lots of such pranks, I guess. As for now, let us keep in mind that this code is nothing but a joke - it ain't a blueprint for action! Smile | :)

Table of authorities

  • C++ Standard
  • Herb Sutter, Exceptional C++ (Addison-Wesley, 2000, ISBN 0-201-61562-2)
  • Internet surfing

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Basil_2

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 1 not impressed PinmemberGiri Ganji9-Nov-11 1:32 
Reason for my vote of 1
not impressed
GeneralBeautifully explained!! PinmemberGPUToaster™24-Oct-11 23:07 
GeneralReason for my vote of 5 Thanks for 10 minutes of fun. PinmemberPablo Aliskevicius24-Oct-11 22:56 
GeneralReason for my vote of 5 a nice one PinmemberKarstenK24-Oct-11 20:38 
QuestionThis thingamajig is called "arrow operator" PinmvpRichard MacCutchan22-Oct-11 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 22 Oct 2011
Article Copyright 2011 by Basil_2
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid