public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/41690]  New: Scoping is incorrect for inherited classes nested inside a templated class.
@ 2009-10-13  0:31 timothy dot renner at gmail dot com
  2009-10-13  1:08 ` [Bug c++/41690] " paolo dot carlini at oracle dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: timothy dot renner at gmail dot com @ 2009-10-13  0:31 UTC (permalink / raw)
  To: gcc-bugs

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 "<built-in>"
# 1 "<command-line>"
# 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 TYPE>
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<int> 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<TYPE>::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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
  2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
@ 2009-10-13  1:08 ` paolo dot carlini at oracle dot com
  2009-10-13  5:48 ` timothy dot renner at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-10-13  1:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from paolo dot carlini at oracle dot com  2009-10-13 01:08 -------
Not a bug, use this->d_data.


-- 

paolo dot carlini at oracle dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
  2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
  2009-10-13  1:08 ` [Bug c++/41690] " paolo dot carlini at oracle dot com
@ 2009-10-13  5:48 ` timothy dot renner at gmail dot com
  2009-10-13  5:56 ` pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: timothy dot renner at gmail dot com @ 2009-10-13  5:48 UTC (permalink / raw)
  To: gcc-bugs



------- 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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
  2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
  2009-10-13  1:08 ` [Bug c++/41690] " paolo dot carlini at oracle dot com
  2009-10-13  5:48 ` timothy dot renner at gmail dot com
@ 2009-10-13  5:56 ` pinskia at gcc dot gnu dot org
  2009-10-13  6:39 ` timothy dot renner at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-10-13  5:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2009-10-13 05:56 -------
>the parent class is not templated. 
Actually it is.

The name of the parent class really is templ<T>::A, which is a templated class.


-- 


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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
  2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
                   ` (2 preceding siblings ...)
  2009-10-13  5:56 ` pinskia at gcc dot gnu dot org
@ 2009-10-13  6:39 ` timothy dot renner at gmail dot com
  2009-10-13 10:26 ` paolo dot carlini at oracle dot com
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: timothy dot renner at gmail dot com @ 2009-10-13  6:39 UTC (permalink / raw)
  To: gcc-bugs



------- 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<T>::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<TYPE> 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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
  2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
                   ` (3 preceding siblings ...)
  2009-10-13  6:39 ` timothy dot renner at gmail dot com
@ 2009-10-13 10:26 ` paolo dot carlini at oracle dot com
  2009-10-13 11:03 ` redi at gcc dot gnu dot org
  2009-10-13 11:56 ` paolo dot carlini at oracle dot com
  6 siblings, 0 replies; 8+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-10-13 10:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from paolo dot carlini at oracle dot com  2009-10-13 10:26 -------
To be clear, the use of this-> isn't a "workaround", is the way C++ as
specified in the current International standard works. This is not the place
for general explanations, you may want to browse D. Vandevoorde and Nicolai M.
Josuttis, "C++ Templates", Addison Wesley, for a clear treatment of the this->,
typename, ::templte, .template, etc., subtleties and *much* more.


-- 


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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
  2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
                   ` (4 preceding siblings ...)
  2009-10-13 10:26 ` paolo dot carlini at oracle dot com
@ 2009-10-13 11:03 ` redi at gcc dot gnu dot org
  2009-10-13 11:56 ` paolo dot carlini at oracle dot com
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu dot org @ 2009-10-13 11:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from redi at gcc dot gnu dot org  2009-10-13 11:02 -------
Or see http://gcc.gnu.org/gcc-3.4/changes.html#cplusplus


-- 


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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/41690] Scoping is incorrect for inherited classes nested inside a templated class.
  2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
                   ` (5 preceding siblings ...)
  2009-10-13 11:03 ` redi at gcc dot gnu dot org
@ 2009-10-13 11:56 ` paolo dot carlini at oracle dot com
  6 siblings, 0 replies; 8+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-10-13 11:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from paolo dot carlini at oracle dot com  2009-10-13 11:55 -------
Yes, Jon. By the way, I also wanted to link to that *very* old changes doc,
probably gave up only because the examples are slightly different. But indeed,
that's exactly the point.


-- 


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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-10-13 11:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-13  0:31 [Bug c++/41690] New: Scoping is incorrect for inherited classes nested inside a templated class timothy dot renner at gmail dot com
2009-10-13  1:08 ` [Bug c++/41690] " paolo dot carlini at oracle dot com
2009-10-13  5:48 ` timothy dot renner at gmail dot com
2009-10-13  5:56 ` pinskia at gcc dot gnu dot org
2009-10-13  6:39 ` timothy dot renner at gmail dot com
2009-10-13 10:26 ` paolo dot carlini at oracle dot com
2009-10-13 11:03 ` redi at gcc dot gnu dot org
2009-10-13 11:56 ` paolo dot carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).