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?