public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/34184]  New: Scope broken for inherited members inside template class?
@ 2007-11-22  5:21 scovich at gmail dot com
  2007-11-22 21:03 ` [Bug c++/34184] " pinskia at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: scovich at gmail dot com @ 2007-11-22  5:21 UTC (permalink / raw)
  To: gcc-bugs

The following code fails to compile 

this.cpp: In member function 'int foo<T>::baz::foo()':
this.cpp:8: error: 'i' was not declared in this scope

// begin this.cpp
template <class T>
struct foo {
  struct bar {
    int i;
  };
  struct baz : bar {
    int foo() { return i; }
  };
};

int main() { }
// end this.cpp

Changing it to 'this->val' solves the problem, but is unwieldy for classes with
lots of members. I'm unsure what the Standard says, but I thought you only
needed 'this->' when the member depends on information the compiler won't have
until template instantiation time. However, that doesn't really apply here --
foo and bar do not depend on the template's type, so the compiler should be
able to figure things out well before the template gets instantiated.

FWIW Sun's CC accepts the code with no warnings. It's usually much more strict
than gcc (to the point of being really frustrating). Even if the Standard says
gcc is right, it would be very convenient if gcc matched CC on this
"extension".


-- 
           Summary: Scope broken for inherited members inside template
                    class?
           Product: gcc
           Version: 4.2.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: scovich at gmail dot com


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


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

* [Bug c++/34184] Scope broken for inherited members inside template class?
  2007-11-22  5:21 [Bug c++/34184] New: Scope broken for inherited members inside template class? scovich at gmail dot com
@ 2007-11-22 21:03 ` pinskia at gcc dot gnu dot org
  2007-11-23  2:07 ` scovich at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-11-22 21:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-11-22 21:03 -------
The issue comes down to if bar is dependent here and if so is baz's base.

The namelookup rules for being dependent are weird and hard to understand
really and actually changes namelookup in some cases so we have to go what the
standard says.


-- 


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


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

* [Bug c++/34184] Scope broken for inherited members inside template class?
  2007-11-22  5:21 [Bug c++/34184] New: Scope broken for inherited members inside template class? scovich at gmail dot com
  2007-11-22 21:03 ` [Bug c++/34184] " pinskia at gcc dot gnu dot org
@ 2007-11-23  2:07 ` scovich at gmail dot com
  2007-12-08 22:51 ` pinskia at gcc dot gnu dot org
  2007-12-11 17:27 ` scovich at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: scovich at gmail dot com @ 2007-11-23  2:07 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from scovich at gmail dot com  2007-11-23 02:06 -------
Subject: Re:  Scope broken for inherited members inside template class?

On 22 Nov 2007 21:03:11 -0000, pinskia at gcc dot gnu dot org
<gcc-bugzilla@gcc.gnu.org> wrote:
> The issue comes down to if bar is dependent here and if so is baz's base.
>
> The namelookup rules for being dependent are weird and hard to understand
> really and actually changes namelookup in some cases so we have to go what the
> standard says.
>

Somehow I'm not surprised the standard is confusing on this point...
my (unqualified) opinion is that bar and baz don't depend on foo's
template parameter. While foo<int>::bar is certainly not the same
class as foo<char>::bar, bar's relationship to baz is always the same
for any instantiation -- part of the template itself.

Imagine that, instead of declaring bar and baz inside "template<class
T> foo" we put them inside an anonymous namespace. What aspect of name
lookup has changed so that baz can suddenly find 'bar::i' without
hand-holding?

I just found an interesting phenomenon -- if baz declares it is "using
bar::i" life is suddenly good. Apparently the compiler is able to
determine that bar is, in fact, a parent class of baz, and that the
using clause is valid. However, it doesn't notice a problem with
"using bar::j" until you actually instantiate a foo<*>::baz. Given
that the compiler does know bar is a base class of baz, shouldn't any
name lookup within baz check for matching members of bar?


-- 


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


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

* [Bug c++/34184] Scope broken for inherited members inside template class?
  2007-11-22  5:21 [Bug c++/34184] New: Scope broken for inherited members inside template class? scovich at gmail dot com
  2007-11-22 21:03 ` [Bug c++/34184] " pinskia at gcc dot gnu dot org
  2007-11-23  2:07 ` scovich at gmail dot com
@ 2007-12-08 22:51 ` pinskia at gcc dot gnu dot org
  2007-12-11 17:27 ` scovich at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-12-08 22:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2007-12-08 22:51 -------
bar in the context of the base class is dependent so the code is invalid.
That is the base class of foo<T>::baz is depdentent.

Note you can declare a specialization of foo<T>::bar which shows that the code
is really dependent.


-- 

pinskia at gcc dot gnu dot org changed:

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


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


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

* [Bug c++/34184] Scope broken for inherited members inside template class?
  2007-11-22  5:21 [Bug c++/34184] New: Scope broken for inherited members inside template class? scovich at gmail dot com
                   ` (2 preceding siblings ...)
  2007-12-08 22:51 ` pinskia at gcc dot gnu dot org
@ 2007-12-11 17:27 ` scovich at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: scovich at gmail dot com @ 2007-12-11 17:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from scovich at gmail dot com  2007-12-11 17:27 -------
(In reply to comment #3)
> Note you can declare a specialization of foo<T>::bar which shows that the code
> is really dependent.
> 

Duh! That's perfect. Thanks.


-- 


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


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

end of thread, other threads:[~2007-12-11 17:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-22  5:21 [Bug c++/34184] New: Scope broken for inherited members inside template class? scovich at gmail dot com
2007-11-22 21:03 ` [Bug c++/34184] " pinskia at gcc dot gnu dot org
2007-11-23  2:07 ` scovich at gmail dot com
2007-12-08 22:51 ` pinskia at gcc dot gnu dot org
2007-12-11 17:27 ` scovich at gmail 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).