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.
If you allow for C++0x, you could return the
decltype
of the sum of an object of type T and C.