llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Access control ignored in member template function

Open llvmbot opened this issue 12 years ago • 3 comments

Bugzilla Link 16410
Version 3.3
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@Myriachan

Extended Description

Access control (e.g. private) seems to be ignored for template functions calling other template functions that do no explicitly instantiate them. Consider the following example:

class Base { private: template <class T> void foo(T t) {} };

class Derived : public Base { public: template <class T> void bar(T t) { foo(t); } };

int main() { Derived d; d.bar(5); }

This compiles (clang version 3.3 (trunk 178896) (llvm/trunk 178895)) even though foo should be inaccessible to Derived.

llvmbot avatar Jun 21 '13 21:06 llvmbot

Still broken in 3.5.0 (tags/RELEASE_350/final)

This still reproduces in post 16 trunk(12e9c7aaa66b7624b5d7666ce2794d912bf9e4b7) and compiles without error

code

class Base {
  private:
    template <class T>
    void foo(T t) {}
};

class Derived : public Base {
  public:
    template <class T>
    void bar(T t) {
      foo(t);
    }
};

int main() {
  Derived d;
  d.bar(5);
}

gcc does not compile it and returns the error

<source>: In instantiation of 'void Derived::bar(T) [with T = int]':
<source>:17:8:   required from here
<source>:11:10: error: 'void Base::foo(T) [with T = int]' is private within this context
   11 |       foo(t);
      |       ~~~^~~
<source>:4:10: note: declared private here
    4 |     void foo(T t) {}
      |          ^~~
Compiler returned: 1

https://godbolt.org/z/coK4jEPxM

wheatman avatar Jun 28 '23 15:06 wheatman

@llvm/issue-subscribers-clang-frontend

llvmbot avatar Jun 28 '23 16:06 llvmbot