public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32762]  New: Lost variable scope with template class that inherits from templated class using args of the first class
@ 2007-07-14  2:25 javimena at gmail dot com
  2007-07-14  5:19 ` [Bug c++/32762] " pinskia at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: javimena at gmail dot com @ 2007-07-14  2:25 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1942 bytes --]

I'm using gcc versión 4.1.2 (Ubuntu 4.1.2-0ubuntu4). I'm trying to compile this
relatively simple code, but it doesn't compiles. The compiler says (i
translated some phrases from spanish to english):

javimena@javimena-desktop:~/cppexamples$ g++ Example1.cc
Example1.cc: In constructor ‘XRIVarArray<Float>::XRIVarArray(int)’:
Example1.cc:37: error: ‘x’ was not declared in this scope


The only file is Example1.cc:
--------------------------------------------------------
// line: 1
#include <cstdio>


template<class Float>
class XRIVar {
public:
  Float min;
  Float max;
};


template<class Var>
class VarArray {
protected:
  Var* x;
public:
  VarArray() {
    x = 0L;
  }
  VarArray(int n) {
    x = new Var[n];
  }

  Var& operator [](int n) {
    return x[n];
  }
};


template<class Float>
class XRIVarArray : public VarArray< XRIVar<Float> > {
public:
  XRIVarArray(int n)
    : VarArray< XRIVar<Float> >(n){
    for (int i=0; i < n; i++) {
      x[i].min = 2;       // line: 37
      x[i].max = 10;
    }
  }
};


int main() {
  using namespace std;

  XRIVarArray<double> xva(5);
  printf( "%g\n", xva[1].max);


  return 0;
}

--------------------------------------------------------

If I inherit from a class without template arguments, the there is no problem.
The workaround for now is to replace x[i] with this->x[i].

But I think this is still a bug.


-- 
           Summary: Lost variable scope with template class that inherits
                    from templated class using args of the first class
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: javimena at gmail dot com
 GCC build triplet:  i486-linux-gnu
  GCC host triplet:  i486-linux-gnu
GCC target triplet:  i486-linux-gnu


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


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

* [Bug c++/32762] Lost variable scope with template class that inherits from templated class using args of the first class
  2007-07-14  2:25 [Bug c++/32762] New: Lost variable scope with template class that inherits from templated class using args of the first class javimena at gmail dot com
@ 2007-07-14  5:19 ` pinskia at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-07-14  5:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-07-14 05:19 -------
> The workaround for now is to replace x[i] with this->x[i].
This is not just a workaround but the correct fix.  the reason is because x is
not dependent so it has to be looked up at definition time and not instantation
time (and definition time does not take into account depdent inherited
classes).  See http://gcc.gnu.org/gcc-3.4/changes.html for more info.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
  GCC build triplet| i486-linux-gnu             |i486-linux-gnu
   GCC host triplet| i486-linux-gnu             |i486-linux-gnu
 GCC target triplet| i486-linux-gnu             |i486-linux-gnu
         Resolution|                            |INVALID


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


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

end of thread, other threads:[~2007-07-14  5:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-14  2:25 [Bug c++/32762] New: Lost variable scope with template class that inherits from templated class using args of the first class javimena at gmail dot com
2007-07-14  5:19 ` [Bug c++/32762] " pinskia at gcc dot gnu dot org

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