究竟为什么会这样被允许:
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
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';
}
幸得约翰内斯·绍布对于上述C ++滥用。 ( 演示 )
您可以访问哪些应该是不可见的,你还有其他的情况? 这只是在标准的一个“错误”?