 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Adrian Sandor Guest
|
Posted: Wed Sep 15, 2004 12:43 am Post subject: problems with templates and function pointers |
|
|
Hello,
Here is a simplified example that shows my problem:
//------8<--------
template
{
template<void(*f)(g&)>void e()
{
f(*this);
}
};
template<class T>void f1(g<T>&x)
{
x.e<f1>(); /* here gcc 3.3.1 says "syntax error before `)' token" */
}
template void f1<bool>(g<bool>&x); /* without this, VC++ at Dinkumware
has an unresolved symbol error; with this line added, it compiles and
links */
int main()
{
g<bool> x;
x.e<f1>(); /* here my VC++ says "cannot convert from 'void (__cdecl
*)(g<T> &)' to 'void (__cdecl *const )(g<T> &)' with [T=bool]" */
}
//------>8----------
Please ignore the infinite recursion and the uselessness of the
program. I'm just talking about compiling/linking errors here. I don't
know if the program can be simplified more while keeping the same
behavior; if you can then please do :)
I tested this program with gcc 3.3.1 (mingw) on my computer, VC++ at
http://www.dinkumware.com/exam/, and VC++.NET 2003 Standard on my
computer.
I included the errors in comments.
Here's what I'd like to know:
- is the program valid C++?
- is it supposed to compile and link correctly without the explicit
f1<bool> instantiation?
- what could be the reason that my VC++ behaves differently from the
one at Dinkumware? (AFAIK it's the same version: .NET 2003)
- are those errors caused by compiler bugs?
- what happens if you try this code on other compilers (e.g. gcc 3.4)?
Thanks
Adrian
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Sep 15, 2004 7:34 pm Post subject: Re: problems with templates and function pointers |
|
|
Adrian Sandor wrote:
| Quote: | Hello,
Here is a simplified example that shows my problem:
//------8<--------
template
{
template<void(*f)(g&)>void e()
{
f(*this);
}
};
template<class T>void f1(g<T>&x)
{
x.e<f1>(); /* here gcc 3.3.1 says "syntax error before `)' token" */
|
You need to add the keyword "template" before "e". See
<http://womble.decadentplace.org.uk/c++/template-faq.html#disambiguation>
for an explanation.
| Quote: | }
template void f1<bool>(g<bool>&x); /* without this, VC++ at Dinkumware
has an unresolved symbol error; with this line added, it compiles and
links */
int main()
{
g<bool> x;
x.e<f1>(); /* here my VC++ says "cannot convert from 'void (__cdecl
*)(g<T> &)' to 'void (__cdecl *const )(g<T> &)' with [T=bool]" */
}
|
That's bizarre. I think the recursive references between the
templates may be confusing it.
| Quote: | //------>8----------
Please ignore the infinite recursion and the uselessness of the
program. I'm just talking about compiling/linking errors here. I don't
know if the program can be simplified more while keeping the same
behavior; if you can then please do :)
I tested this program with gcc 3.3.1 (mingw) on my computer, VC++ at
http://www.dinkumware.com/exam/, and VC++.NET 2003 Standard on my
computer.
I included the errors in comments.
Here's what I'd like to know:
- is the program valid C++?
|
No, but I believe it would be with the addition of "template" as I
noted above.
| Quote: | - is it supposed to compile and link correctly without the explicit
f1<bool> instantiation?
|
I believe so.
| Quote: | - what could be the reason that my VC++ behaves differently from the
one at Dinkumware? (AFAIK it's the same version: .NET 2003)
|
Perhaps they are using different compiler options, though I can't
think of any that would affect template instantiation.
| Quote: | - are those errors caused by compiler bugs?
|
I believe VC++'s error message indicates a bug.
| Quote: | - what happens if you try this code on other compilers (e.g. gcc 3.4)?
|
Comeau 4.3.3 beta accepts the code with the addition of "template".
--
Ben Hutchings
Quantity is no substitute for quality, but it's the only one we've got.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Adrian Sandor Guest
|
Posted: Thu Sep 16, 2004 11:08 am Post subject: Re: problems with templates and function pointers |
|
|
So it is "x.template e<f1>();"
Thank you very much, that solved it in gcc!
I didn't know about this syntax, always learning
And by the way, good FAQ, it's bookmarked now :)
| Quote: | - is it supposed to compile and link correctly without the explicit
f1<bool> instantiation?
I believe so.
|
gcc agrees with that, VC++ at Dinkumware doesn't
Thanks
Adrian
[ 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 Sep 17, 2004 2:13 am Post subject: Re: problems with templates and function pointers |
|
|
[email]aditsu (AT) gmail (DOT) com[/email] (Adrian Sandor) wrote in message news:<83dae161.0409141137.79437303 (AT) posting (DOT) google.com>...
| Quote: | Hello,
Here is a simplified example that shows my problem:
//------8<--------
template
{
template<void(*f)(g&)>void e()
{
f(*this);
}
};
template<class T>void f1(g<T>&x)
{
x.e<f1>(); /* here gcc 3.3.1 says "syntax error before `)' token" */
}
|
i've encountered this before and believe it to be a bug in gcc. I
couln't find a workaround either.
<...>
jon
[ 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
|
|