本文介绍了模板参数中的访问控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
受此答案的启发,它声称颠覆了访问控制系统,我编写了以下最低版本的hack
Inspired from this answer, which claims to subvert the access control system, I wrote the following minimal version of the hack
template<typename T>
inline T memptr{};
template<auto Val>
struct setptr
{
struct setter { setter() { memptr<decltype(Val)> = Val; } };
static setter s;
};
template<auto Val>
typename setptr<Val>::setter setptr<Val>::s{};
然后是用作
class S
{
int i;
};
template struct setptr<&S::i>;
auto no_privacy(S& s)
{
return s.*memptr<int S::*>;
}
为什么 template struct setptr<& S :: i> ;;
为什么不违反访问控制?
Why doesn't template struct setptr<&S::i>;
violate access control?
是因为 [class.access]
具体不包括实例化吗?在这种情况下,为什么不包含实例化?
specifically doesn't include instantiations? In which case, why doesn't it include instantiations?
推荐答案
来自.spec]/6 (重点是我):
因此,您看到的这项技术滥用了此规则,主要是为了允许具有私有类型或其他私有实体的类的实现模板(例如特征)的实现者
So this technique you see abuses this rule, which is primarely there to allow implementers of a class specialize templates (such as traits) with private types or other private entities
这篇关于模板参数中的访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!