C++ Challenge: Maximum unique reserved words in one logical line of code?

647 views Asked by At

I have a perennial thought experiment about how to write a line of C++ code that has the maximum number of unique reserved words in it. In this challenge, you can duplicate keywords as much as you'd like, but all that matters is the number of unique keywords you use. For example, if you write

void MyFunction(int, int, int, int);

There are four instances of int, but the above line has a score of 2 because it only has two unique keywords in it (void and int, namely). This line, though,

void MyFunction(int, double, short, long);

Has a score of 5 for its five reserved words.

So far, the best I've been able to come up with is

export template <typename T, class C>
inline void DiabolicalFunc (int, char, short, 
                            long, double, signed, 
                            unsigned, bool, float,
                            wchar_t, const int,
                            volatile int, 
                            enum MyEnum,
                            void* (*)(size_t) = &(operator new), 
                            void (*)(void*) = &(operator delete), 
                            int = const_cast<int*>(static_cast<const int *>(reinterpret_cast<int*>(0))), 
                            void* = dynamic_cast<void*>(reinterpret_cast<ios_base*>(0)),
                            bool = true, bool = false, int = sizeof(int), 
                            const std::type_info& = typeid(int),
                            struct MyStruct = MyStruct(), union MyUnion = MyUnion(), 
                            int = 0 and 0,
                            int = 0 bitand 0,
                            int = 0 bitor 0, 
                            int = compl 0, 
                            int = not 0, 
                            int = 0 not_eq 0,
                            int = 0 or 0, 
                            int = 0 xor 0) throw();

This has a whopping 39 reserved words in it. It assumes that you have defined an enum MyEnum, struct MyStruct, and union MyUnion before declaring it, though. And yes, this does compile in g++ if you set up the appropriate types before using it (though it does give a warning about export.)

I'm curious if anyone sees any way to cram even more unique keywords into a line of code. Can someone top my example? Or find a way to make it Even More Diabolical?

I know that C++ is freeform so a "line of code" is not a really good measure of structure, but I think we can come to a reasonable interpretation of what this means.

EDIT: Just added throw() to the end of the function to get one more keyword in there.

3

There are 3 answers

1
Michael Aaron Safyan On

If you allow for C++0x, you could return the decltype of the sum of an object of type T and C.

4
shoosh On

if you add a body to the function you can add some register and auto variables, a try {} catch() {} block, a switch with a break and a return and almost all other C++ keywords
This is really an exercise in futility.

0
GManNickG On

A line of C++ code you say?

// asm auto bool break case catch char class const ...

I kid, I kid.