public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Type problem with template functions and inheritance ?
@ 2004-09-08 20:10 Simon MARTIN
  2004-09-08 21:33 ` Vaclav Haisman
  0 siblings, 1 reply; 3+ messages in thread
From: Simon MARTIN @ 2004-09-08 20:10 UTC (permalink / raw)
  To: gcc

Hello.

I played a little with templates and template functions, and I encountered a 
problem with GCC while doing so. The code at the end of this message builds 
with Microsoft's Visual C++ Toolkit 2003, but fails with GCC. Since the code 
is quite "weird", I wonder whether there's a problem in GCC or Visual is too 
laxist.

What is strange is:
  - the "<unknown type>" in the error message
  - the fact that if I "help" GCC a little (before the first static_cast), 
then things work fine.

Thanks in advance for your help / explanation.

Regards,
Simon

[simon@texel gcc_test]$ g++ -v
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.3.2/specs
Configured with: ../configure --prefix=/usr --libdir=/usr/lib 
--with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info 
--enable-shared --enable-threads=posix --disable-checking --enable-long-long 
--enable-__cxa_atexit --enable-clocale=gnu 
--enable-languages=c,c++,ada,f77,objc,java,pascal 
--host=i586-mandrake-linux-gnu --with-system-zlib
Thread model: posix
gcc version 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)

[simon@texel gcc_test]$ g++ -Wall -o test test.cpp
test.cpp: In function `int main(int, char**)':
test.cpp:18: error: invalid static_cast from type `<unknown type>' to type 
`bool (Base::*)(const char*)'

==== test.cpp
class Base {};
struct MyClass : public Base
{
  template<int key> bool Method(const char *value) { return true; }
};

int main(int argc, char *argv[])
{
  typedef bool (Base::*BaseMethodPtr)(const char*);
  typedef bool (MyClass::*MethodPtr)(const char*);

  MyClass aClass;

  MethodPtr mp = &MyClass::Method<1>;
  BaseMethodPtr p1 = static_cast<BaseMethodPtr>(mp);
  (aClass.*p1)("One");

  BaseMethodPtr p2 = static_cast<BaseMethodPtr>(&MyClass::Method<1>);
  (aClass.*p2)("OneAgain");

  return 0;
}
==== end of test.cpp

-- 
Kile - an Integrated LaTeX Environment for KDE
http://kile.sourceforge.net

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Type problem with template functions and inheritance ?
  2004-09-08 20:10 Type problem with template functions and inheritance ? Simon MARTIN
@ 2004-09-08 21:33 ` Vaclav Haisman
  0 siblings, 0 replies; 3+ messages in thread
From: Vaclav Haisman @ 2004-09-08 21:33 UTC (permalink / raw)
  To: Simon MARTIN; +Cc: gcc


Because Comeau's test drive compiler accepts I would fill a bug report in
Bugzilla if I were you.

VH


On Wed, 8 Sep 2004, Simon MARTIN wrote:

> Hello.
>
> I played a little with templates and template functions, and I encountered a
> problem with GCC while doing so. The code at the end of this message builds
> with Microsoft's Visual C++ Toolkit 2003, but fails with GCC. Since the code
> is quite "weird", I wonder whether there's a problem in GCC or Visual is too
> laxist.
>
> What is strange is:
>   - the "<unknown type>" in the error message
>   - the fact that if I "help" GCC a little (before the first static_cast),
> then things work fine.
>
> Thanks in advance for your help / explanation.
>
> Regards,
> Simon
>
> [simon@texel gcc_test]$ g++ -v
> Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.3.2/specs
> Configured with: ../configure --prefix=/usr --libdir=/usr/lib
> --with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info
> --enable-shared --enable-threads=posix --disable-checking --enable-long-long
> --enable-__cxa_atexit --enable-clocale=gnu
> --enable-languages=c,c++,ada,f77,objc,java,pascal
> --host=i586-mandrake-linux-gnu --with-system-zlib
> Thread model: posix
> gcc version 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)
>
> [simon@texel gcc_test]$ g++ -Wall -o test test.cpp
> test.cpp: In function `int main(int, char**)':
> test.cpp:18: error: invalid static_cast from type `<unknown type>' to type
> `bool (Base::*)(const char*)'
>
> ==== test.cpp
> class Base {};
> struct MyClass : public Base
> {
>   template<int key> bool Method(const char *value) { return true; }
> };
>
> int main(int argc, char *argv[])
> {
>   typedef bool (Base::*BaseMethodPtr)(const char*);
>   typedef bool (MyClass::*MethodPtr)(const char*);
>
>   MyClass aClass;
>
>   MethodPtr mp = &MyClass::Method<1>;
>   BaseMethodPtr p1 = static_cast<BaseMethodPtr>(mp);
>   (aClass.*p1)("One");
>
>   BaseMethodPtr p2 = static_cast<BaseMethodPtr>(&MyClass::Method<1>);
>   (aClass.*p2)("OneAgain");
>
>   return 0;
> }
> ==== end of test.cpp
>
> --
> Kile - an Integrated LaTeX Environment for KDE
> http://kile.sourceforge.net
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Type problem with template functions and inheritance?
@ 2004-09-08 22:41 Wolfgang Bangerth
  0 siblings, 0 replies; 3+ messages in thread
From: Wolfgang Bangerth @ 2004-09-08 22:41 UTC (permalink / raw)
  To: Simon MARTIN, gcc


> I played a little with templates and template functions, and I encountered a
> problem with GCC while doing so. The code at the end of this message builds
> with Microsoft's Visual C++ Toolkit 2003, but fails with GCC. Since the code
> is quite "weird", I wonder whether there's a problem in GCC or Visual is too
> laxist.
>
> What is strange is:
>   - the "<unknown type>" in the error message
>   - the fact that if I "help" GCC a little (before the first static_cast),
> then things work fine.

I just opened PR 17359 for this. It seems to me that gcc is wrong indeed.

W.

-------------------------------------------------------------------------
Wolfgang Bangerth              email:            bangerth@ices.utexas.edu
                               www: http://www.ices.utexas.edu/~bangerth/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-08 22:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-08 20:10 Type problem with template functions and inheritance ? Simon MARTIN
2004-09-08 21:33 ` Vaclav Haisman
2004-09-08 22:41 Type problem with template functions and inheritance? Wolfgang Bangerth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).