This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/12429] New: Template class Foo<X> cannot access private members of Foo<Y>


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12429

           Summary: Template class Foo<X> cannot access private members of
                    Foo<Y>
           Product: gcc
           Version: 3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sean dot gies at discreet dot com
                CC: gcc-bugs at gcc dot gnu dot org

Member functions of template classes cannot access protected or private members of a different 
specialization of itself, nor can a friendship be declared to allow the access.

The only workaround involves hacking the public interface of the template class.

For example, private member function Bar(int) of class Foo<float> cannot call Bar(int) of class 
Foo<int>:

template <class T> class Foo {
        template <class F> friend class Foo<F>; // This attempted work-around also fails
        void Bar(int x=0) {if (x<4) Foo<int>().Bar(x+1);}
public:
        void Test() {Bar();}
};
int main(int argc, char** argv) {
        Foo<float>().Test();
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]