查找 class 成员 C++ 的保护级别
Find the protection level of class member C++
有没有办法知道会员的保护级别是多少?
我正在创建一个单例 base-class,我想确保 child class 将其 constructor/destructor 声明为 private/protected。我该怎么做?
template<class c>
class singleton
{
static_assert(std::is_private<&c::c> // does this exist?
|| std::is_protected<&c::c>);
static_assert(std::is_private<&c::~c>
|| std::is_protected<&c::~c>);
};
很遗憾,您无法执行您的操作。到目前为止,C++还没有足够强大的反射特性。
实际上,程序员使用单例 类 不需要像这样的保护机制,因为 C++ 不是为最大编译时验证而设计的。在 C++ 中还有很多其他类似的事情是您不能做的 — 换句话说,C++ 不可能满足用户可能期望的所有可能功能,尤其是那些通常被认为没有用的功能。提出反射是因为很多人觉得它有用,但到目前为止,C++ 中还没有包含等效的功能。
在 2018, (and later 2019). In fact, it appears it was circulated in June of 2019 as part of the Committee's proposal process. However, I'm not really sure what happened to it (other then I think I found another revision 中有一篇论文提出了这一点。
一些编译器可能会实验性地支持它。如果他们这样做了,我相信你会使用的header是<experimental/reflect>
(至少根据论文)。
否则,你有点卡住了。它看起来是 reflection was deferred to a later standard,所以 C++ 中并不存在这样的东西。希望它将进入 C++23。但现在我们必须等待。
有没有办法知道会员的保护级别是多少?
我正在创建一个单例 base-class,我想确保 child class 将其 constructor/destructor 声明为 private/protected。我该怎么做?
template<class c>
class singleton
{
static_assert(std::is_private<&c::c> // does this exist?
|| std::is_protected<&c::c>);
static_assert(std::is_private<&c::~c>
|| std::is_protected<&c::~c>);
};
很遗憾,您无法执行您的操作。到目前为止,C++还没有足够强大的反射特性。
实际上,程序员使用单例 类 不需要像这样的保护机制,因为 C++ 不是为最大编译时验证而设计的。在 C++ 中还有很多其他类似的事情是您不能做的 — 换句话说,C++ 不可能满足用户可能期望的所有可能功能,尤其是那些通常被认为没有用的功能。提出反射是因为很多人觉得它有用,但到目前为止,C++ 中还没有包含等效的功能。
在 2018, (and later 2019). In fact, it appears it was circulated in June of 2019 as part of the Committee's proposal process. However, I'm not really sure what happened to it (other then I think I found another revision 中有一篇论文提出了这一点。
一些编译器可能会实验性地支持它。如果他们这样做了,我相信你会使用的header是<experimental/reflect>
(至少根据论文)。
否则,你有点卡住了。它看起来是 reflection was deferred to a later standard,所以 C++ 中并不存在这样的东西。希望它将进入 C++23。但现在我们必须等待。