Форум » C/C++ » Различие в объявлении friend классов в стандартах C++ 2003 и 2011 и баг компилятора GCC 4.8.1 » Ответить

Различие в объявлении friend классов в стандартах C++ 2003 и 2011 и баг компилятора GCC 4.8.1

Сыроежка: Случайно для себя обнаружил следующее различие в описании дружественных классов (friend classes) в стандартах C++ 2003 и C++ 2011. В разделе "11.4 Friends" стандарта C++ 2003 в параграфе №2 написано следующее: [quote]This also means that private and protected type names from the class granting friendship can be used in the base-clause of a nested class of the friend class. However, the declarations of members of classes nested within the friend class cannot access the names of private and protected members from the class granting friendship. Also, because the base-clause of the friend class is not part of its member declarations, the base-clause of the friend class cannot access the names of the private and protected members from the class granting friendship. [/quote] И затем приводится пример: [pre2] class A { class B { }; friend class X; }; class X : A::B { // ill-formed: A::B cannot be accessed // in the base-clause for X A::B mx; // OK: A::B used to declare member of X class Y : A::B { // OK: A::B used to declare member of X A::B my; // ill-formed: A::B cannot be accessed // to declare members of nested class of X }; }; [/pre2] Обратите внимание на два комментария, которые начинаются со слов ill-formed Теперь обратимся к стандарту C++ 2011. В разделе "11.3 Friends" в параграфе №2 уже написано следующее [quote]2 Declaring a class to be a friend implies that the names of private and protected members from the class granting friendship can be accessed in the base-specifiers and member declarations of the befriended class. [/quote] И тот же самый пример, что приведен выше, содержит уже другие комментарии: [pre2] class A { class B { }; friend class X; }; struct X : A::B { // OK: A::B accessible to friend A::B mx; // OK: A::B accessible to member of friend class Y { A::B my; // OK: A::B accessible to nested member of friend }; }; [/pre2] Как видно, новый стандарт разрешает использовать в объявлении базовых членов и вложенных членов данных класса закрытые и защищенные члены класса, являющегося дружественным к данному классу, Если использовать on-line компилятор Microsoft , то приведенный пример из стандарта компилируется успешно. Однако если тот же самый пример попробовать скомпилировать с помощью on-line компилятора GCC 4.8.1 , то будет выдано следующее сообщение об ошибке: [pre2] prog.cpp:5:11: error: ‘class A::B’ is private class B { }; ^ prog.cpp:9:14: error: within this context class X : A::B ^ [/pre2] То есть GCC 4.8.1 не позволяет использовать закрытые члены дружественного класса в объявлении базовых членов.

Ответов - 0



полная версия страницы