public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/11694] New: Template member class of template class mix-up
@ 2003-07-28 13:55 cholm at nbi dot dk
  2003-07-28 14:08 ` [Bug c++/11694] " pinskia at physics dot uc dot edu
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: cholm at nbi dot dk @ 2003-07-28 13:55 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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

           Summary: Template member class of template class mix-up
           Product: gcc
           Version: 3.2.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: cholm at nbi dot dk
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu

Hi GCC developers,

I think I stumbled on a bug in G++.  It has to do with template nested classes 
in template classes.  It seems that there's something missing in the parse. 
Maybe it has something to do with the `second template pass bug'? 

Please find the file `baz.cc' below.

  prompt> g++-3.2 baz.cc -o baz
   g++-3.2 baz.cc -o baz
  baz.cc: In instantiation of `bar<double>':
  baz.cc:47:   instantiated from here
  baz.cc:20: internal error: Segmentation fault
  Please submit a full bug report,
  with preprocessed source if appropriate.
  See <URL:http://www.gnu.org/software/gcc/bugs.html> for instructions.

My machine is a dual Intel Celeron, running Linux 2.4.18 SMP, with
GLibC version 2.2.5-11.  The distribution is Debian GNU/Linux 3.0
(woody) with some extra packages from testing (sarge).

GCC is:

  prompt> gcc-3.2 -v
  Reading specs from /usr/lib/gcc-lib/i386-linux/3.2.2/specs
  Configured with: ../src/configure -v --enable-
languages=c,c++,java,f77,proto,p
ascal,objc,ada --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info -
-with-gxx-include-dir=/usr/include/c++/3.2 --enable-shared --with-system-zlib -
-
enable-nls --without-included-gettext --disable-__cxa_atexit --enable-java-
gc=bo
ehm --enable-objc-gc i386-linux
  Thread model: posix
  gcc version 3.2.2

Note, that the `baz.cc' file compiles fine with Intel C++ version
7.0.

If I change the source, so that the `bar' class takes two template
parameters, like:

  template <typename A, typename B=A>
  struct bar : public foo<A, B>
  {
    typedef foo<A , B> base_type;
    typedef base_type::helper<1> helper_type;
    helper_type _help;

    bar(const A& a) : base_type(a, a), _help() {}
    virtual int doit() { return _help.doit(_a,_b); }
  };

it compiles fine.

If I in the `baz' class changes the definition of `helper_type' to
directly reference the sub-class of `foo<A,B>', like:

  template <typename A>
  struct baz : public bar<A>
  {
    typedef bar<A>              base_type;
    typedef foo<A,A>::helper<1> helper_type;
    helper_type* _helper;
    baz() : base_type(42) { _helper = new helper_type(2); }
    int doit() { return _helper->doit(_a,_b); }
  };

then I get:

  > g++-3.2 baz.cc -o baz
  bar.cc: In constructor `baz<A>::baz() [with A = double]':
  bar.cc:50:   instantiated from here
  bar.cc:36: invalid use of undefined type `struct foo<double, B>::helper<1>'
  bar.cc:6: declaration of `struct foo<double, B>::helper<1>'
  bar.cc: In member function `int baz<A>::doit() [with A = double]':
  bar.cc:51:   instantiated from here
  bar.cc:37: no matching function for call to `foo<double, B>::helper<1>::doit(
     double&, double&)'

Again, Intel C++ version 7.0 compiles that just fine

Only if I instancises `foo<A,B>::helper<C>' with explicit types for
`A' and `B', like:

  template <typename A>
  struct baz : public bar<A>
  {
    typedef bar<A>              base_type;
    typedef foo<double,double>::helper<1> helper_type;
    helper_type* _helper;
    baz() : base_type(42) { _helper = new helper_type(2); }
    int doit() { return _helper->doit(_a,_b); }
  };

can I get an error free compile

The contens of `baz.cc' follows below:
//====================================================================
template <typename A, typename B>
struct foo {
  template <int C>
  struct helper
  {
    helper() {}
    int doit(const A& a, const B& b) { return C + int(a) + int(b); }
  };
  A _a;
  B _b;
  foo() : _a(0), _b(0) {}
  foo(const A& a, const B& b) : _a(a), _b(b) {}
  virtual int doit() { return 0; }
};

//====================================================================
template <typename A>
struct bar : public foo<A, A>
{
  typedef foo<A , A> base_type;
  typedef base_type::helper<1> helper_type;
  helper_type _help;

  bar(const A& a) : base_type(a, a), _help() {}
  virtual int doit() { return _help.doit(_a,_b); }
};

//====================================================================
template <typename A>
struct baz : public bar<A>
{
  typedef bar<A>                          base_type;
  typedef typename base_type::helper_type helper_type;
  helper_type* _helper;
  baz() : base_type(42) { _helper = new helper_type(); }
  int doit() { return _helper->doit(_a,_b); }
};

//====================================================================
int
main(int argc, char** argv)
{

  foo<double, double> foo(3.14, 42);

  bar<double> b1(3.14);
  int a1 = b1.doit();

  baz<double> b2;
  int a2 = b2.doit();

  return 0;
}

//
// EOF of `baz.cc'
//

Yours, 

 ___  |  Christian Holm Christensen
  |_| |  -------------------------------------------------------------
    | |  Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|           DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|            Denmark                    Office: (+45) 353  25 305
 ____|   Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
 | |


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

end of thread, other threads:[~2003-10-19 22:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-28 13:55 [Bug c++/11694] New: Template member class of template class mix-up cholm at nbi dot dk
2003-07-28 14:08 ` [Bug c++/11694] " pinskia at physics dot uc dot edu
2003-07-29  9:57 ` [Bug c++/11694] ICE with missing typename/template keywords ehrhardt at mathematik dot uni-ulm dot de
2003-07-29 12:16 ` [Bug c++/11694] [3.3 Regression] " pinskia at physics dot uc dot edu
2003-10-16  9:24 ` mmitchel at gcc dot gnu dot org
2003-10-19 22:18 ` 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).