I just happened to find that a nested private template class can be accessed directly outside the enclosing class using a using
directive:
class wrapper
{
private:
template <typename T>
class __tklass {};
class __klass {};
};
template <typename T>
using tklass = wrapper::__tklass<T>; // Expected error but compiles OK
// using klass = wrapper::__klass; // "Error: __klass is private"
int main()
{
tklass<int> v1; // Expected error but compiles OK
// wrapper::__tklass<int> v3; // "Error: __tklass is private"
// wrapper::__klass v4; // "Error: __klass is private"
}
The lines marked "Error: __xxx is private" correctly report an error when uncommented. But the lines with tklass
get compiled without any complaint from the compiler.
So why exactly doesn't the compiler flag tklass
as error despite wrapper::__tklass
being private? Is it by any chance allowed by the standard? If so, wouldn't that be considered a serious access violation?
I tried this on gcc-4.9.2, clang-3.5.0 and visual studio 2013 express. GCC command line:
g++ -std=c++11 -pedantic -Wall -Wextra -Wshadow myfile.cpp