[Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class.
When a nested class inherits from another nested class, scoping is incorrect only if the outer class is templated. A full .ii file follows, but to illustrate in a compact example: class A { class B { protected: int d_data; }; class C : public B { void foo() { d_data = 0; } }; }; If A is *not* templated, the code will compile. If A is templated, the compiler complains that d_data has not been declared in the current scope. There is no option for attaching files, so the .ii file follows: --%< # 1 "a.cpp" # 1 "" # 1 "" # 1 "a.cpp" # 1 "templ.h" 1 class notempl { private: int d_stuff; class A { protected: int d_data; }; class B : public A { void test() { d_data = 0; } }; }; template class templ { private: int d_stuff; class A { protected: int d_data; }; class B : public A { void test() { d_data = 0; } }; }; # 2 "a.cpp" 2 int main() { notempl tmp1; templ tmp2; return 0; } --%< Detailed compilation follows: ~/tmp/gcc-bug# g++ -v -save-temps a.cpp Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic' /usr/lib/gcc/x86_64-linux-gnu/4.3.3/cc1plus -E -quiet -v -D_GNU_SOURCE a.cpp -D_FORTIFY_SOURCE=2 -mtune=generic -fpch-preprocess -o a.ii ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.3.3/../../../../x86_64-linux-gnu/include" ignoring nonexistent directory "/usr/include/x86_64-linux-gnu" #include "..." search starts here: #include <...> search starts here: /usr/include/c++/4.3 /usr/include/c++/4.3/x86_64-linux-gnu /usr/include/c++/4.3/backward /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.3.3/include /usr/lib/gcc/x86_64-linux-gnu/4.3.3/include-fixed /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic' /usr/lib/gcc/x86_64-linux-gnu/4.3.3/cc1plus -fpreprocessed a.ii -quiet -dumpbase a.cpp -mtune=generic -auxbase a -version -fstack-protector -o a.s GNU C++ (Ubuntu 4.3.3-5ubuntu4) version 4.3.3 (x86_64-linux-gnu) compiled by GNU C version 4.3.3, GMP version 4.2.4, MPFR version 2.4.0. GGC heuristics: --param ggc-min-expand=97 --param ggc-min-heapsize=126404 Compiler executable checksum: d9dd67dcc191271ac69fd2c05266cf9c In file included from a.cpp:2: templ.h: In member function void templ::B::test(): templ.h:31: error: d_data was not declared in this scope -- Summary: Scoping is incorrect for inherited classes nested inside a templated class. Product: gcc Version: 4.3.3 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: timothy dot renner at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41690
[Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
--- Comment #2 from timothy dot renner at gmail dot com 2009-10-13 05:47 --- Thank you, I found that I could access it that way, and it makes a good workaround. I'm curious why this is not a scoping bug though? It seems like the compiler should still be able to scope d_data from the parent class, especially as it works when the parent class is not templated. If that's not true, could you please explain it to me? I'd like to understand this. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41690
[Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
--- Comment #4 from timothy dot renner at gmail dot com 2009-10-13 06:38 --- >Actually it is. > >The name of the parent class really is templ::A, which is a templated class. Sorry, my mistake, I mistyped that. Let me clarify. I didn't mean the parent class, I meant the containing class. I'm not sure what the technical name is for the class that houses a nested class. Classes A and B nested in class templ are definitely templated, but what about classes A and B nested in class notempl? What I am curious about is that the compiler does recognize inheritance in a templated class: If I added public constructors to both A and B: A() {} and B() : A() {} A() is accessibile by class B, and that code will compile, but the compiler does not recognize that d_data from A is accessible by B when templation is involved, but does recognize it when templation is not involved. I can see it being a more complicated case to parse correctly, but the current behavior seems inconsistent to me and like something the compiler should not throw an error on, so if there is a good reason why it should work the way it currently does, I'd really like to learn what it is. If not, this bug should be reopened and kept around at the very least as a "nice to have" feature since it's easily worked around, or just flat out marked as a won't fix. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41690