Ten Different Consecutive Keywords

Ten different consecutive keywords is the (non-canonical) name of an exercise posed to the reader by BjarneStroustrup in TheCeePlusPlusProgrammingLanguage. The wording is thus:

Exercise 15.8
Write a standards-conforming C++ program containing a sequence of at least ten different consecutive keywords not separated by identifiers, operators, punctuation characters, etc.

Note that "alternate tokens" for operators like "and", "or" etc. do not count. There are separate challenges that permit their use, though.

Scroll down for a spoiler.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

My original solution looks like this:

 int main() {
   if (0);
   else do return delete new const volatile long signed int(0), 0; while (0);
 }
This is a correct CeePlusPlus program that is semantically equivalent to /bin/true in UnixOs.

The line "if (0);" just exists so that the next line can use "else". The semicolon represents an empty statement, which is a NoOp. If you find this confusing, the empty block "{}" would do the same thing.

The second line consists of the "else" which executes a TrivialDoWhileLoop.

The do-while loop contains a single return statement. Since it isn't a block, braces can be omitted.

The return statement returns 0, after evaluating "delete new const volatile long signed int(0)" for SideEffects. This is accomplished through the use of the comma operator.

"delete new const volatile long signed int(0)" creates a new primitive of type "const volatile long signed int" on the heap, initializes it with zero, and immediately deletes it again. The sole purpose of this is to add 7 more consecutive keywords.

There are several other approaches, which all happen to look quite similar (notwithstanding variations in the order of type qualifiers or minor differences like "long" vs. "short" and "signed" vs. "unsigned"). However, "throw sizeof" is often used instead of "return delete". One clever person introduced "return" as an eleventh keyword in that type of solution, again, through the use of the comma operator:

 ... else do return throw sizeof new signed long int const volatile(), 0 ...
Note that in this solution, no zero is returned, since the SideEffect (throws an exception) of "throw" precedes evaluation of the return statement. Some memory is leaked, but this is permissible.

Unfortunately, the braces around a try block are mandatory (even if only one statement is "tried"), otherwise something like

 ... else do try return throw sizeof new signed long int const volatile(), 0; catch(...){}; while(0); ...
would be legal CeePlusPlus.


What is the sense of this exercise? -- HelmutLeitner

See SmugCeePlusPlusWeenie for a potential answer. This exercise is similar to programming in something resembling LambdaCalculus while in a LispLanguage environment.

See also ObfuscatedCee and InternationalObfuscatedCeeCodeContest


CategoryCpp


EditText of this page (last edited May 6, 2005) or FindPage with title or text search