template function in inner class

  • Thread starter Christof Warlich
  • Start date
C

Christof Warlich

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
 
L

Lionel B

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 said:

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

Christof Warlich

Lionel said:
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
 
L

Lionel B

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

Triple-DES

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>();}
};

?
 
J

James Kanze

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

Christof Warlich

Triple-DES said:
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 :).
 
C

Christof Warlich

James said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top