Is this a defect of C++ that allow to break access control?

73 Views Asked by At

Declaring a member private means there should never be a legal way to access it directly from outside. But template explicit instantiation breaks the law. What's the consideration of this? Or it's a defect?

Example:

#include <cassert>
#include <iostream>

class A {
 public:
  int X() { return x_; }

 private:
  int x_;
};

int A::*FiledPtr();
template <int A::*M>
struct Rob {
  friend int A::*FiledPtr() { return M; }
};
template struct Rob<&A::x_>;

int main() {
  A o;
  o.*FiledPtr() = 10;
  assert(o.X() == 10);
}
0

There are 0 best solutions below