C++ 缺乏的等效PHP的self关键字,其评估的封闭类的类型.
在每个类的基础上伪造它很容易:
struct Foo
{
typedef Foo self;
};
Run Code Online (Sandbox Code Playgroud)
但我不得不再写Foo一遍.也许有一天我会弄错,导致一个无声的错误.
我可以使用一些decltype朋友和朋友的组合来"自主地"完成这项工作吗?我已尝试过以下内容但this在该地方无效:
struct Foo
{
typedef decltype(*this) self;
};
// main.cpp:3:22: error: invalid use of 'this' at top level
// typedef decltype(*this) self;
Run Code Online (Sandbox Code Playgroud)
(我不会担心相同的static,相同但后期绑定.)