lamefun
lamefun

Reputation: 4252

Is public usage of private typedef portable?

class Settings
{
private:
    typedef std::map<std::string, SettingsOption> OptionMap;

public:
    typedef OptionMap::iterator iterator;
    typedef OptionMap::const_iterator const_iterator;

    ...
};

Is this code portable? What does the standard state about it?

Upvotes: 18

Views: 4292

Answers (1)

CB Bailey
CB Bailey

Reputation: 792657

It's legal and Settings::iterator and Settings::const_iterator are accessible to all users of the Settings class.

Access control in C++ is applied purely to names. There's a note and example in ISO/IEC 14882:2011 11 [class.access]/4 that makes it clear that this is the intention.

[...] [ Note: Because access control applies to names, if access control is applied to a typedef name, only the accessibility of the typedef name itself is considered. The accessibility of the entity referred to by the typedef is not considered. For example,

class A {
  class B { };
public:
  typedef B BB;
};

void f() {
  A::BB x; // OK, typedef name A::BB is public
  A::B y; // access error, A::B is private
}

end note ]

Upvotes: 25

Related Questions