 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dominique Marcadet Guest
|
Posted: Sun Feb 29, 2004 4:01 am Post subject: template function called in template class |
|
|
Given the following code :
struct A
{
template < typename T >
T a() { return T(0); }
};
template < typename Z >
struct B
{
void b()
{
A a0;
void (A::*f)() = &A::a<int>; // 1a
int i = a0.*f(); // 1b
int j = a0.a<int>(); // 2
}
};
g++ (tested with gcc 3.3 on Mac OS X 10.3.2 and gcc 3.3.3) gives me a
:
parse error before `;' token
on line //2 (but it is OK on lines //1a and //1b)
IMHO, it's a bug of gcc, but before submitting it to the g++ team, I
would like to know if I am not wrong in my analysis.
NB: if B is not a template class, errors occured first at line //1a
and I am unable to find any reason.
Thank you in advance for your help,
Dominique
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dominique Marcadet Guest
|
Posted: Sun Feb 29, 2004 4:46 pm Post subject: Re: template function called in template class |
|
|
I respond to myself after having found the answer (I should have
search a bit further in the group).
| Quote: | struct A
{
template < typename T
T a() { return T(0); }
};
template < typename Z
struct B
{
void b()
{
A a0;
void (A::*f)() = &A::a
int i = a0.*f(); // 1b
int j = a0.a<int>(); // 2
|
To avoid ambiguity, you have to tell the compiler that a is a template
:
int j = a0.template a<int>(); // 2
(found in the thread "compile problem with template (gcc-2.95.x,gcc
3.0, Comeau)")
| Quote: | }
};
NB: if B is not a template class, errors occured first at line //1a
and I am unable to find any reason.
|
After correcting line // 1a:
int (A::*f)() = &A::a<int>; // 1a
the line 1b should be written as :
int i = (a0.*f)(); // 1b
(found answer in thread "another pointer to member function
question").
Dominique
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephen C. Dewhurst Guest
|
Posted: Sun Feb 29, 2004 4:54 pm Post subject: Re: template function called in template class |
|
|
On 28 Feb 2004 23:01:48 -0500, [email]Dominique.Marcadet (AT) supelec (DOT) fr[/email]
(Dominique Marcadet) wrote:
| Quote: |
IMHO, it's a bug of gcc, but before submitting it to the g++ team, I
would like to know if I am not wrong in my analysis.
|
I think gcc is right on this:
template < typename Z >
struct B
{
void b()
{
A a0;
int (A::*f)() = &A::a<int>; // return type was void
// should be int
int i = (a0.*f)(); // missing parens
int j = (a0.a<int>)(); // missing parens
}
};
Steve
Steve Dewhurst
www.semantics.org
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sun Feb 29, 2004 5:40 pm Post subject: Re: template function called in template class |
|
|
Dominique Marcadet <Dominique.Marcadet (AT) supelec (DOT) fr> wrote:
| Quote: | Given the following code :
struct A
{
template < typename T
T a() { return T(0); }
};
template < typename Z
struct B
{
void b()
{
A a0;
void (A::*f)() = &A::a
int i = a0.*f(); // 1b
int j = a0.a<int>(); // 2
}
};
g++ (tested with gcc 3.3 on Mac OS X 10.3.2 and gcc 3.3.3) gives me a
:
parse error before `;' token
on line //2 (but it is OK on lines //1a and //1b)
IMHO, it's a bug of gcc, but before submitting it to the g++ team, I
would like to know if I am not wrong in my analysis.
NB: if B is not a template class, errors occured first at line //1a
and I am unable to find any reason.
Thank you in advance for your help,
Dominique
using CW 5.3 on this old macos 8.6 machine [cw 9.x is on my 'office |
panter machine] it says that 1a is a 'unimpliemented feature, could be
gcc is lost in space?? Will test this during week on mwc92 but I think
it might have a problem.
if I did this cw 5.3 reported no problem. I am assuming that this is
not the solution desired. but it may solve the problem for the given
code.
template <class T>
struct A
{
T a() { return T(0); }
};
template < typename Z >
struct B
{
void b()
{
A<int> a0;
int (A<int>::*f)() = &A<int>::a; // 1a
int i = (a0.*f)(); // 1b
int j = a0.a(); // 2
}
};
struct C
{
B<int> bar;
void foo(){bar.b();}
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Sun Feb 29, 2004 5:44 pm Post subject: Re: template function called in template class |
|
|
[email]Dominique.Marcadet (AT) supelec (DOT) fr[/email] (Dominique Marcadet) writes:
| Quote: | Given the following code :
struct A
{
template < typename T
T a() { return T(0); }
};
template < typename Z
struct B
{
void b()
{
A a0;
void (A::*f)() = &A::a
int i = a0.*f(); // 1b
int j = a0.a<int>(); // 2
}
};
g++ (tested with gcc 3.3 on Mac OS X 10.3.2 and gcc 3.3.3) gives me a
:
parse error before `;' token
on line //2 (but it is OK on lines //1a and //1b)
IMHO, it's a bug of gcc, but before submitting it to the g++ team, I
would like to know if I am not wrong in my analysis.
|
It is a known bug in the parser of any version of GCC prior to 3.4.0
(not yet released, but is upcoming). A workaround is to rewrite
line // 2 as:
int j = a0.a::template<int>();
The infamous "::template" is not required, but it is allowed in that
context.
--
Gabriel Dos Reis
[email]gdr (AT) cs (DOT) tamu.edu[/email]
Texas A&M University -- Computer Science Department
301, Bright Building -- College Station, TX 77843-3112
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephen C. Dewhurst Guest
|
Posted: Sun Feb 29, 2004 11:57 pm Post subject: Re: template function called in template class |
|
|
On 29 Feb 2004 11:46:10 -0500, [email]Dominique.Marcadet (AT) supelec (DOT) fr[/email]
(Dominique Marcadet) wrote:
| Quote: | struct A
{
template < typename T
T a() { return T(0); }
};
template < typename Z
struct B
{
void b()
{
A a0;
void (A::*f)() = &A::a
int i = a0.*f(); // 1b
int j = a0.a<int>(); // 2
To avoid ambiguity, you have to tell the compiler that a is a template
:
int j = a0.template a<int>(); // 2
|
Actually, that's not the particular problem here, since the compiler
is able to determine that A::a is a template by inspection. However,
it would be a problem, and the "template" keyword required if A were
itself a template, and the A template was being used in a context
where its instantiation arguments were not known precisely:
template <typename X>
struct A
{
template < typename T >
T a() { return T(0); }
};
template < typename Y, typename Z >
struct B
{
void b()
{
A<Y> a0;
//...
Z j = a0.template a<Z>(); // now we need "template"
//...
Steve
Steve Dewhurst
www.semantics.org
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Mon Mar 01, 2004 12:04 am Post subject: Re: template function called in template class |
|
|
Gabriel Dos Reis wrote in news:m33c8uxkx6.fsf (AT) merlin (DOT) cs.tamu.edu:
| Quote: | Dominique.Marcadet (AT) supelec (DOT) fr (Dominique Marcadet) write
| int j = a0.a<int>(); // 2
It is a known bug in the parser of any version of GCC prior to 3.4.0
(not yet released, but is upcoming). A workaround is to rewrite
line // 2 as:
int j = a0.a::template<int>();
|
A typo I think:
int j = a0.template a< int >();
| Quote: |
The infamous "::template" is not required, but it is allowed in that
context.
|
Rob.
--
http://www.victim-prime.dsl.pipex.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Mar 01, 2004 12:25 pm Post subject: Re: template function called in template class |
|
|
Rob Williscroft <rtw (AT) freenet (DOT) REMOVE.co.uk> writes:
| Quote: | Gabriel Dos Reis wrote in news:m33c8uxkx6.fsf (AT) merlin (DOT) cs.tamu.edu:
[email]Dominique.Marcadet (AT) supelec (DOT) fr[/email] (Dominique Marcadet) write
| int j = a0.a<int>(); // 2
It is a known bug in the parser of any version of GCC prior to 3.4.0
(not yet released, but is upcoming). A workaround is to rewrite
line // 2 as:
int j = a0.a::template<int>();
A typo I think:
int j = a0.template a< int >();
|
Sigh. You're absolutely correct. I should not have been posting when
it was obvsiously time to go to bed.
--
Gabriel Dos Reis
[email]gdr (AT) cs (DOT) tamu.edu[/email]
Texas A&M University -- Computer Science Department
301, Bright Building -- College Station, TX 77843-3112
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|