Why is it legal to inappropriately access privates in an explicit instantiation?

白昼怎懂夜的黑 提交于 2019-12-03 12:06:42

问题


Why on earth would this be allowed:

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
template<typename T>
struct invisible
{
    static typename T::type value;
};

template<typename T>
typename T::type invisible<T>::value;

//////////////////////////////////////////////////////////////////////////
template<typename T, typename T::type P>
class construct_invisible
{
    construct_invisible(){ invisible<T>::value = P; }
    static const construct_invisible instance;
};

template<typename T, typename T::type P>
const construct_invisible<T, P> construct_invisible<T, P>::instance;

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
class A
{
public:
    A(int x) : m_X(x){}
private:
    int m_X;
};

//////////////////////////////////////////////////////////////////////////
struct A_x{ typedef int A::*type; };
template class construct_invisible<A_x, &A::m_X>;// <---- WHY DOES `&A::m_X` WORK HERE?

//////////////////////////////////////////////////////////////////////////
int main()
{
    A a(17);
    std::cout << a.*invisible<A_x>::value << '\n';
}

Credit goes to Johannes Schaub for the above C++ abuse. (Demo)

Are there other cases you can access what should be invisible to you? Is this just a 'bug' in the standard?


回答1:


It is so that the author of the class that has the private member can explicitly instantiate that member or pass it as an argument as you just did.

The compiler has no idea who is in front of the keyboard, so the access checking here is rather conservative.

Parameters used in explicit instantiation get special treatment because there is no mechanism for a class author to explicitly instantiate a template in an allowed context or to somehow permit doing so with a friend declaration.



来源:https://stackoverflow.com/questions/12048193/why-is-it-legal-to-inappropriately-access-privates-in-an-explicit-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!