Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 662
David Avatar asked Aug 21 '12 03:08

David


1 Answers

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.

like image 52
Johannes Schaub - litb Avatar answered Nov 09 '22 16:11

Johannes Schaub - litb