This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

G++ cannot use nested classes in template functions


PROBLEM:
    I'm currently using the STL and g++, and I decided to had write
template
functions to search maps, and vectors etc using the nested iterator
classes of
map, vector etc. To save writing different functions for each type of
map or
vector (which had the exact same functionality) I decided
to write a generic template function that could be used to search on the
passed
in map or vector.
    However when I declared the nested const_iterator of either a map or
list I
got a syntax error from g++. From this I basically discovered I could
not use
nested classes (and probably types) of the template type in a template
function.
    Below I include a short example of  to show what happpens. Included
is the
example is all the g++  output  as comments.
   I have also attached this file along with the .i and .s files.

Regards
    David Smith/

EXAMPLE:
class Test
{
   public:
      class Inside
      { };
};

void f1()
{
  Test::Inside i; // <<< --- works
}

template <class T>
void f(const T& t)
{
  T::Inside i; // <<< ----- Compiler Error

  // G++ info
  // --------

  // $ g++ -v --save-temps  -c main.cc -o main.o
  // Reading specs from
/usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs
  // gcc version 2.8.1
  //  /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/cpp -lang-c++ -v
-undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -D__GNUC_MINOR__=8
-Dsparc -Dsun -Dunix -D__svr4__ -D__SVR4 -D__sparc__ -D__sun__
-D__unix__ -D__svr4__ -D__SVR4 -D__sparc -D__sun -D__unix -Asystem(unix)
-Asystem(svr4) -D__EXCEPTIONS -D__GCC_NEW_VARARGS__ -Acpu(sparc)
-Amachine(sparc) main.cc main.ii
  // GNU CPP version 2.8.1 (sparc)
  // #include "..." search starts here:
  // #include <...> search starts here:
  //  /usr/local/include
  //  /usr/local/sparc-sun-solaris2.6/include
  //  /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/include
  //  /usr/include
  // End of search list.
  //  /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/cc1plus main.ii
-quiet -dumpbase main.cc -version -o main.s
  // GNU C++ version 2.8.1 (sparc-sun-solaris2.6) compiled by GNU C
version 2.8.1.
  // main.cc: In function `void f(const T &)':
  // main.cc:16: parse error before `;'
  // *** Error code 1
  // make: Fatal error: Command failed for target `main.o'

  // Machine details
  // $ uname -a
  // SunOS amy 5.6 Generic_105181-12 sun4u sparc SUNW,Ultra-4
  //
  // g++ STL version
  // g++-3
}





main.cc

# 1 "main.cc"
class Test
{
   public:
      class Inside
      { };
};

void f1()
{
  Test::Inside i;
}

template <class T>
void f(const T& t)
{
  T::Inside i;  

   
   
  
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

   
   
   
   
   
   
}

	.file	"main.cc"
gcc2_compiled.:
.section	".text"
	.align 4
	.global f1__Fv
	.type	 f1__Fv,#function
	.proc	020
f1__Fv:
.LLFB1:
	!#PROLOGUE# 0
	save %sp,-120,%sp
.LLCFI0:
	!#PROLOGUE# 1
	b .LL2
	nop
	b .LL1
	nop
.LL2:
.LL1:
	ret
	restore
.LLFE1:
.LLfe1:
	.size	 f1__Fv,.LLfe1-f1__Fv

.section	".eh_frame",#alloc,#write
__FRAME_BEGIN__:
	.uaword	.LLECIE1-.LLSCIE1
.LLSCIE1:
	.uaword	0x0
	.byte	0x1
	.byte	0x0
	.byte	0x1
	.byte	0x7c
	.byte	0x65
	.byte	0xc
	.byte	0xe
	.byte	0x0
	.byte	0x9
	.byte	0x65
	.byte	0xf
	.align 4
.LLECIE1:
	.uaword	.LLEFDE1-.LLSFDE1
.LLSFDE1:
	.uaword	.LLSFDE1-__FRAME_BEGIN__
	.uaword	.LLFB1
	.uaword	.LLFE1-.LLFB1
	.byte	0x4
	.uaword	.LLCFI0-.LLFB1
	.byte	0xd
	.byte	0x1e
	.byte	0x2d
	.byte	0x9
	.byte	0x65
	.byte	0x1f
	.align 4
.LLEFDE1:
	.ident	"GCC: (GNU) 2.8.1"
begin:vcard 
n:Smith;David
tel;home:0181 292 1136
tel;work:0181 633 3065
x-mozilla-html:FALSE
adr:;;;;;;
version:2.1
email;internet:smithd@pia.bt.co.uk
fn:David Smith
end:vcard

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]