C++挑战:一行逻辑代码中唯一保留字的最大数量?
我一直有一个关于如何编写一行具有最大数量的唯一保留字的 C++ 代码的思想实验。在此挑战中,您可以根据需要重复关键字,但重要的是您使用的唯一关键字的数量。例如,如果您编写
void MyFunction(int, int, int, int);
There are four instants of int,但上面的行得分为 2,因为它只有两个唯一关键字(void
和 int
,即)。不过,这一行的
void MyFunction(int, double, short, long);
5 个保留字的得分为 5。
到目前为止,我能想到的最好的办法是
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 中有多达 39 个保留字。不过,它假定您在声明之前已经定义了 enum MyEnum
、struct MyStruct
和 union MyUnion
。是的,如果您在使用它之前设置了适当的类型,那么它确实会在g++
中进行编译(尽管它确实会给出有关导出
的警告。)
我很好奇是否有人能找到任何方法将更多独特的关键字塞入一行代码中。有人可以超越我的例子吗?或者找到一种方法让它变得更加邪恶?
我知道 C++ 是自由形式的,因此“一行代码”并不是一个很好的结构衡量标准,但我认为我们可以对这意味着什么做出合理的解释。
编辑:刚刚在函数末尾添加了throw()
,以在其中再添加一个关键字。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说的是一行C++代码?
我开玩笑,我开玩笑。
A line of C++ code you say?
I kid, I kid.
如果允许 C++0x,则可以返回类型 T 和 C 的对象之和的
decltype
。If you allow for C++0x, you could return the
decltype
of the sum of an object of type T and C.如果您向函数添加主体,则可以添加一些
register
和auto
变量、一个try {} catch() {}
块、一个switch
带有break
和return
以及几乎所有其他 C++ 关键字这实在是徒劳的练习。
if you add a body to the function you can add some
register
andauto
variables, atry {} catch() {}
block, aswitch
with abreak
and areturn
and almost all other C++ keywordsThis is really an exercise in futility.