 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dan Grose Guest
|
Posted: Tue Aug 17, 2004 7:22 pm Post subject: Templated member functions - help needed |
|
|
Hi
Cannot get the folowing to compile ( g++ (GCC) 3.2.3 )
template <class T1>
class A
{
public:
template <class T2>
T2* foo() { return new T2; }
};
class B
{
public:
template <class T>
void bar()
{
A<double> a;
a.foo<T>(); // hmmm - does not compile
}
};
int main()
{
A<double> a;
a.foo<int>(); // okay - compiles
return 0;
}
compiler output as follows
g++ mincrim.cpp
mincrim.cpp: In member function `void B::bar()':
mincrim.cpp:19: syntax error before `;' token
Any help much appreciated.
Dan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Tue Aug 17, 2004 9:59 pm Post subject: Re: Templated member functions - help needed |
|
|
Dan Grose <grose (AT) wt (DOT) tno.nl> writes:
| Quote: | template <class T1
class A
{
public:
template
T2* foo() { return new T2; }
};
class B
{
public:
template
void bar()
{
A
a.foo<T>(); // hmmm - does not compile
|
a.template foo<T>();
Note that at the time the compiler sees the definition of this member
function, it doesn't know if there will be a specialization of A for double
that will declare the name foo for something different than a member
function template.
Informing the compiler taht foo always names a template allows it to better
parse the code and, potentially, give better diagnostic messages.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Aug 17, 2004 10:02 pm Post subject: Re: Templated member functions - help needed |
|
|
Dan Grose wrote:
| Quote: | Cannot get the folowing to compile ( g++ (GCC) 3.2.3 )
template <class T1
class A
{
public:
template
T2* foo() { return new T2; }
};
class B
{
public:
template
void bar()
{
A
a.foo<T>(); // hmmm - does not compile
|
g++ compiles
a.template foo<T>(); // 'template' is needed
(although I am not sure whether it's required by the C++ Standard
or not -- too lazy to look)
| Quote: | }
};
int main()
{
A<double> a;
a.foo<int>(); // okay - compiles
return 0;
}
compiler output as follows
g++ mincrim.cpp
mincrim.cpp: In member function `void B::bar()':
mincrim.cpp:19: syntax error before `;' token
Any help much appreciated.
|
HTH
Victor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Richard Corden Guest
|
Posted: Wed Aug 18, 2004 6:14 pm Post subject: Re: Templated member functions - help needed |
|
|
Dan Grose <grose (AT) wt (DOT) tno.nl> writes:
| Quote: | Hi
Cannot get the folowing to compile ( g++ (GCC) 3.2.3 )
template <class T1
class A
{
public:
template
T2* foo() { return new T2; }
};
class B
{
public:
template
void bar()
{
A
a.foo<T>(); // hmmm - does not compile
}
};
int main()
{
A<double> a;
a.foo<int>(); // okay - compiles
return 0;
}
compiler output as follows
g++ mincrim.cpp
mincrim.cpp: In member function `void B::bar()':
mincrim.cpp:19: syntax error before `;' token
|
Although other posters here have mentioned using the 'template'
keyword I don't believe that it is strictly neceesary for the above
example.
14.2/4 says:
When the name of a member template specialization appears after . or
-> in a postfix-expression, or after nested-name-specifier in a
qualified-id, and the postfix-expression or qualified-id explicitly
depends on a template-parameter (14.6.2), the member template name
must be prefixed by the keyword template. Otherwise the name is
assumed to name a non-template.
In the above example 'a' is the postfix-expression and it does not
explicitly depend on a template-parameter. Therefore, we don't need
to add the 'template' keyword.
In more recent revisions of the standard it explicitly states that the
template keyword is allowed where it is not strictly necessary.
However, I do remember a question as to whether the 1998 standard
would allow an unnecessary 'template' keyword, so you might suffer
portability problems adding it for the above example.
Regards,
Richard
--
Richard Corden
To reply remove 's' from address
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Hopkin Guest
|
Posted: Wed Aug 18, 2004 6:33 pm Post subject: Re: Templated member functions - help needed |
|
|
Thomas Maeder <maeder (AT) glue (DOT) ch> wrote
| Quote: |
Note that at the time the compiler sees the definition of this member
function, it doesn't know if there will be a specialization of A for double
that will declare the name foo for something different than a member
function template.
|
Actually, 'template' is strictly redundant here. Any subsequent
specialisations of A would make the code illegal.
It appears that explicitly putting template makes the code more
portable to older compilers. For maximum portability, I'd go for
#include <boost/config.hpp>
a.BOOST_NESTED_TEMPLATE foo<T>();
Cheers,
James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jon hanson Guest
|
Posted: Fri Aug 20, 2004 12:54 am Post subject: Re: Templated member functions - help needed |
|
|
I experienced a similar error using gcc 3.2.2. In my case the
templated method had an int template parameter and it compiled ok in a
normal function, but failed inside a templated function. If i changed
the method to take an argument i got this error :-
invalid use of member (did you forget the `&' ?)
I think if you change you're foo() method to not be templated it will
compile.
After searching the gcc archives i found some mails which mentioned
this as a bug in gcc.
jon
Dan Grose <grose (AT) wt (DOT) tno.nl> wrote
| Quote: | Hi
Cannot get the folowing to compile ( g++ (GCC) 3.2.3 )
template <class T1
class A
{
public:
template
T2* foo() { return new T2; }
};
class B
{
public:
template
void bar()
{
A
a.foo<T>(); // hmmm - does not compile
}
};
int main()
{
A<double> a;
a.foo<int>(); // okay - compiles
return 0;
}
compiler output as follows
g++ mincrim.cpp
mincrim.cpp: In member function `void B::bar()':
mincrim.cpp:19: syntax error before `;' token
Any help much appreciated.
Dan
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Fri Aug 20, 2004 10:15 am Post subject: Re: Templated member functions - help needed |
|
|
Richard Corden <richards_corden (AT) hotmail (DOT) com> writes:
| Quote: | Dan Grose <grose (AT) wt (DOT) tno.nl> writes:
Hi
Cannot get the folowing to compile ( g++ (GCC) 3.2.3 )
template <class T1
class A
{
public:
template
T2* foo() { return new T2; }
};
class B
{
public:
template
void bar()
{
A
a.foo<T>(); // hmmm - does not compile
}
};
int main()
{
A<double> a;
a.foo<int>(); // okay - compiles
return 0;
}
compiler output as follows
g++ mincrim.cpp
mincrim.cpp: In member function `void B::bar()':
mincrim.cpp:19: syntax error before `;' token
Although other posters here have mentioned using the 'template'
keyword I don't believe that it is strictly neceesary for the above
example.
|
It is not necessary, however, gcc prior to 3.4 offers it as a
work-around for the fact that it cannot correctly parse
'a.foo<T>()' in some examples. gcc 3.4 can parse 'a.foo<T>()'
correctly in all cases, and so no longer requires it.
| Quote: |
14.2/4 says:
When the name of a member template specialization appears after . or
-> in a postfix-expression, or after nested-name-specifier in a
qualified-id, and the postfix-expression or qualified-id explicitly
depends on a template-parameter (14.6.2), the member template name
must be prefixed by the keyword template. Otherwise the name is
assumed to name a non-template.
In the above example 'a' is the postfix-expression and it does not
explicitly depend on a template-parameter. Therefore, we don't need
to add the 'template' keyword.
In more recent revisions of the standard it explicitly states that the
template keyword is allowed where it is not strictly necessary.
However, I do remember a question as to whether the 1998 standard
would allow an unnecessary 'template' keyword, so you might suffer
portability problems adding it for the above example.
|
There are some compilers that reject it. But gcc prior to 3.4
requires it. That's where the money in porting is. :-)
[ 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
|
|