Triggered by this question I was wondering if there is a legal hack to get an alias to a private nested type (note that the question was about something completely different and also this one is not "how can I access something declared as private?"). Inspired by this answer my first step was to write this:
class foo {
struct bar{};
public: void foobar(const bar&){}
};
struct hack {
template <typename T>
operator T() { return {}; }
};
int main() {
foo f;
//f.foobar(foo::bar());// NOPE: bar is private !!
f.foobar(hack()); // hm, bar is not private?
}
And I have to admit, I am already lost. Why is it allowed to let a template deduce a private type? Also considering that
struct no_hack {
operator foo::bar(){ return {}; }
};
is not allowed (foo::bar
is private).
Note that I am not asking what is the motivation behind allowing the hack
but rather what parts of the standard can explain this difference? Is type deduction not restricted by private
?