Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is public usage of private typedef portable?

Tags:

c++

typedef

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?

like image 406
lamefun Avatar asked Dec 03 '11 08:12

lamefun


1 Answers

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 ]

like image 133
CB Bailey Avatar answered Oct 31 '22 19:10

CB Bailey