C++ question template resolve pointer through non-templated base class

andre maute andre.maute@gmx.de
Fri Apr 2 13:46:00 GMT 2010


Comeau C++ compiles your Code without the A:: prefixes.
Perhaps you need a syntax lawyer here.



In general,
remove the ";" at the end of the curly braces "}" of the function defintions.



On Tuesday 30 March 2010, RusMor wrote:
>   Dear reader,
>
>   I have a question  about an interesting feature of gcc (version etc. see
> below).
>
>   The "problem" is demonstrated with this minimal code:
>
> #include <iostream>
> #include <vector>
>
> using namespace std;
>
> class A
> {
> public:
>   template <typename AType> void Fun () {};
> };
>
> class Base
> {
> public:
>   A * GetA() { return new A; };
> };
>
> class DerivedNotTemplated : public Base
> {
> public:
>   void Fun ()
>   {
>     this->GetA()->/*A::*/Fun<int>();  // Don't need the A:: here
>   };
> };
>
> template <typename BType>
> class DerivedTemplated : public Base
> {
> public:
>   void Fun ()
>   {
>     this->GetA()->A::Fun<BType>();   // Why need the "A::" ??
>   };
> };
>
> int main ()
> {
>   return 1;
> };
>
>   If I omit the "A::" in the definition of DerivedTemplated<BType>::Fun(),
> I get the following compiler error:
>
> ytsen@moniac:~/scratch> g++ test.templated.templated.C
> test.templated.templated.C: In member function ‘void
> DerivedTemplated<BType>::Fun()’:
> test.templated.templated.C:35: error: expected primary-expression before
> ‘>’ token
> test.templated.templated.C:35: error: expected primary-expression before
> ‘)’ token
>
>   But in the non-templated case this does not happen. My question is "why"
> ??
>
>   Many thanks in advance!!
>
>   Cheers,
>
> Ytsen.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ytsen@moniac:~/scratch> gcc -v
> Using built-in specs.
> Target: x86_64-suse-linux
> Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
> --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
> --enable-languages=c,c++,objc,fortran,obj-c++,java,ada
> --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.4
> --enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/
> --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap
> --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit
> --enable-libstdcxx-allocator=new --disable-libstdcxx-pch
> --enable-version-specific-runtime-libs --program-suffix=-4.4
> --enable-linux-futex --without-system-libunwind --with-arch-32=i586
> --with-tune=generic --build=x86_64-suse-linux
> Thread model: posix
> gcc version 4.4.1 [gcc-4_4-branch revision 150839] (SUSE Linux)
> ytsen@moniac:~/scratch> uname -a
> Linux moniac 2.6.31.8-0.1-desktop #1 SMP PREEMPT 2009-12-15 23:55:40 +0100
> x86_64 x86_64 x86_64 GNU/Linux








More information about the Gcc-help mailing list