Writing sonnets in C++

Recently, I came across this post – Write a URL in a C++ program, one of those C’s tricks. If you have not already read the post, I will wait until you read and return …

waitingWaiting …

The crux of the trick is the protocol part of the URL – http:, becomes a goto label and the rest of the URL starting with // becomes a comment. Sweet 🤗

My turn My turn …

Let me tell you that you can write a Shakespeare sonnet in C++. You can compile the following program (no compilation errors) and ship it without a worry!

[code lang=cpp]
#include <iostream>

using namespace std;

template<typename T>
int BeSureNotToUseThisFunction(T i, T j)
// You can swap with any poem or verse you like!
Shall I compare thee to a summer’s day?
Thou art more lovely and more temperate.
Rough winds do shake the darling buds of May,
And summer’s lease hath all too short a date.
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimmed;
And every fair from fair sometime declines,
By chance, or nature’s changing course, untrimmed;
But thy eternal summer shall not fade,
Nor lose possession of that fair thou ow’st,
Nor shall death brag thou wand’rest in his shade,
When in eternal lines to Time thou grow’st.
So long as men can breathe, or eyes can see,
So long lives this, and this gives life to thee.

return i + j;

int main()
cout << "Initialing ….";

// All your application logic goes here!
// Mine is …
for (int i = 0; i < 5; ++i)
cout << i << std::endl;

return 0;

Touché? I bet you know why the above will work … per the name given to the template function.

Know of any such clever tricks? Reminds me of Crazy Braces, although it is not a trick at all but solid valid C++ code.

  • Image Courtesy: IconFinder
  • A trick is a trick. The trick above works for Visual C++ compiler but not in gcc.
Show Comments