[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

template function in inner class

Christof Warlich

10/7/2008 8:42:00 AM

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
7 Answers

jt

10/7/2008 8:57:00 AM

0

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

Christof Warlich

10/7/2008 10:39:00 AM

0

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


jt

10/7/2008 11:00:00 AM

0

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

Triple-DES

10/7/2008 11:02:00 AM

0

On 7 Okt, 12:38, Christof Warlich <cwarl...@alcatel-lucent.de> 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>();}
};

?





James Kanze

10/7/2008 11:43:00 AM

0

On Oct 7, 10:41 am, Christof Warlich <cwarl...@alcatel-lucent.de>
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:james.kanze@gmail.com
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

Christof Warlich

10/8/2008 6:51:00 AM

0

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

Christof Warlich

10/8/2008 6:54:00 AM

0

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.