[Code]
template <class T>
int doSomething(T const &aT)
   {
   return aT.a();
   }

class A
   {
   public:
      int a() const
         {
         return 1;
         }
   };

class B
   {
   public:
      int b() const
         {
         return 1;
         }
   };

template <class T>
inline bool isOne(T const &aT)
   {
   return doSomething(aT) == 1;
   }

int doSomething(int aI)
   {
   return aI;
   }

int doSomething(B const &aB)
   {
   return aB.b();
   }

int
main(int argc, char *argv[])
   {
   A a;
   isOne(a);

   B b;
   isOne(b);

   isOne(1);

   return 0;
   }
[/Code]

This code returns this error:
template1.cc: In function &#8216;int doSomething(const T&) [with T =
int]&#8217;:
template1.cc:28:30:   instantiated from &#8216;bool isOne(const T&) [with T =
int]&#8217;
template1.cc:50:11:   instantiated from here
template1.cc:4:16: error: request for member &#8216;a&#8217; in
&#8216;aT&#8217;, which is of non-class type &#8216;const int&#8217;

This bug is similar to bug 23885, but the difference is that there is template
function isOne instantioned in main and all is defined before main. But right
doSomething is defined after isOne function. Forward declaration helps.

When using class templates it is ok:
[Code]
template <class T>
struct DoTraits
   {
   static int doSomething(T const &aT)
      {
      return aT.a();
      }
   };

class A
   {
   public:
      int a() const
         {
         return 1;
         }
   };

class B
   {
   public:
      int b() const
         {
         return 1;
         }
   };

template <class T>
bool isOne(T const &aT)
   {
   return DoTraits<T>::doSomething(aT) == 1;
   }

template <>
struct DoTraits<int>
   {
   static int doSomething(int const &aI)
      {
      return aI;
      }
   };

template <>
struct DoTraits<B>
   {
   static int doSomething(B const &aB)
      {
      return aB.b();
      }
   };

int
main(int argc, char *argv[])
   {
   A a;
   isOne(a);

   B b;
   isOne(b);

   isOne(1);

   return 0;
   }
[/Code]

Sun Studio 11 reports no error on this code (MSVC is working with similar (real
app) code - not tested with this example).

Failing compilers:
/tmp/gcc/usr/bin/g++ -v:
Using built-in specs.
COLLECT_GCC=/tmp/gcc/usr/bin/g++
COLLECT_LTO_WRAPPER=/tmp/gcc/usr/libexec/gcc/i686-pc-linux-gnu/4.5.0/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ../gcc-4.5-20091203/configure --prefix=/tmp/gcc/usr
--with-local-prefix=/tmp/gcc/usr --enable-clocale=gnu --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-languages=c,c++
--disable-libstdcxx-pch --disable-multilib --disable-bootstrap
Thread model: posix
gcc version 4.5.0 20091203 (experimental) (GCC)


g++ -v:
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.4-6'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --with-tune=generic --enable-checking=release
--build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.4 (Debian 4.3.4-6)


-- 
           Summary: g++ instantiates function templates at the wrong place
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jezz at hkfree dot org
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42281

Reply via email to