Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template function in inner class

Reply
Thread Tools

template function in inner class

 
 
Christof Warlich
Guest
Posts: n/a
 
      10-07-2008
Hi,

could anyone tell what's wrong with this?:

template<int x>
struct Outer {
struct Inner {
template<int y> void finner() {}
};
Inner inner;
void fouter() {inner.finner<0>();}
};

Thanks,

Christof
 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      10-07-2008
On Tue, 07 Oct 2008 10:41:59 +0200, Christof Warlich wrote:

> Hi,
>
> could anyone tell what's wrong with this?:
>
> template<int x>
> struct Outer {
> struct Inner {
> template<int y> void finner() {}
> };
> Inner inner;
> void fouter() {inner.finner<0>();}


void fouter() {inner.template finner<0>();}

> };


compiles for me. I'm not clear on why this might be required.

--
Lionel B
 
Reply With Quote
 
 
 
 
Christof Warlich
Guest
Posts: n/a
 
      10-07-2008
Lionel B schrieb:
> compiles for me. I'm not clear on why this might be required.


It doesn't compile with my gcc:

$ g++ --version
g++ (GCC) 3.4.6
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -c tst.cc
tst.cc: In member function `void Outer<x>::fouter()':
tst.cc:8: error: expected primary-expression before ')' token

So this may be a compiler bug?! I'll try with a more recent gcc version
tonight. Which compiler did you use?

Thanks and regards,

Christof


 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      10-07-2008
On Tue, 07 Oct 2008 12:38:43 +0200, Christof Warlich wrote:

> Lionel B schrieb:
>> compiles for me. I'm not clear on why this might be required.

>
> It doesn't compile with my gcc:
>
> $ g++ --version
> g++ (GCC) 3.4.6
> Copyright (C) 2006 Free Software Foundation, Inc. This is free software;
> see the source for copying conditions. There is NO warranty; not even
> for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ g++ -c tst.cc
> tst.cc: In member function `void Outer<x>::fouter()': tst.cc:8: error:
> expected primary-expression before ')' token
>
> So this may be a compiler bug?! I'll try with a more recent gcc version
> tonight. Which compiler did you use?


Odd. The amended code compiles for me here with gcc 3.4.6 (also gcc
4.3.1, 4.3.0 4.1.2 and Intel icc 10.0).

All the above gcc versions reject your original code, although icc
accepts it.

Comeau (about as standards-compliant as it gets) rejects your original
code with:

"ComeauTest.c", line 7: error: expected an expression
void fouter() {inner.finner<0>();}

and accepts the amended version.

--
Lionel B
 
Reply With Quote
 
Triple-DES
Guest
Posts: n/a
 
      10-07-2008
On 7 Okt, 12:38, Christof Warlich <(E-Mail Removed)> wrote:
> Lionel B schrieb:
>
> > compiles for me. I'm not clear on why this might be required.

>
> It doesn't compile with my gcc:
>
> $ g++ --version
> g++ (GCC) 3.4.6
> Copyright (C) 2006 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. *There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> $ g++ -c tst.cc
> tst.cc: In member function `void Outer<x>::fouter()':
> tst.cc:8: error: expected primary-expression before ')' token
>
> So this may be a compiler bug?! I'll try with a more recent gcc version
> tonight. Which compiler did you use?
>
> Thanks and regards,
>
> Christof


Did you try:

template<int x>
struct Outer {
struct Inner {
template<int y> void finner() {}
};
Inner inner;
void fouter() {inner.template finner<0>();}
};

?





 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-07-2008
On Oct 7, 10:41 am, Christof Warlich <(E-Mail Removed)>
wrote:

> could anyone tell what's wrong with this?:


> template<int x>
> struct Outer {
> struct Inner {
> template<int y> void finner() {}
> };
> Inner inner;
> void fouter() {inner.finner<0>();}


The < here is a less than operator. I doubt that that's what
you wanted. You need to tell the compiler that inner.finner is
a template, e.g.:

inner. template finner< 0 >() ;

(In earlier compilers, this might work, because they don't
actually parse the template until instantiation. The standard
is designed, however, so that they can, and without knowing the
instantiation type, it can't know what inner.finner refers to.
In order to parse correctly, however, the compiler must know
which symbols name types, and which name templates. So you
occasionally have to insert typename or template to keep it
happy.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Christof Warlich
Guest
Posts: n/a
 
      10-08-2008
Triple-DES schrieb:
> Did you try:
>
> template<int x>
> struct Outer {
> struct Inner {
> template<int y> void finner() {}
> };
> Inner inner;
> void fouter() {inner.template finner<0>();}
> };

No, I missed Lionel's change. With the change, it works
for me as well .
 
Reply With Quote
 
Christof Warlich
Guest
Posts: n/a
 
      10-08-2008
James Kanze schrieb:
> The < here is a less than operator. I doubt that that's what
> you wanted. You need to tell the compiler that inner.finner is
> a template, e.g.:
>
> inner. template finner< 0 >() ;
>
> (In earlier compilers, this might work, because they don't
> actually parse the template until instantiation. The standard
> is designed, however, so that they can, and without knowing the
> instantiation type, it can't know what inner.finner refers to.
> In order to parse correctly, however, the compiler must know
> which symbols name types, and which name templates. So you
> occasionally have to insert typename or template to keep it
> happy.)


.... and thanks for the background information on why the template keyword
is needed here.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with inner class of a class template as member function returntype Fei Liu C++ 4 01-08-2008 07:35 PM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments