This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Parse error with member templates


I've run into something rather surprising. When trying to put together some sample code to demonstrate a bug in a different compiler, I got a parse error with GCC (I tried 3.0.4 and 3.2). Trying to compile the attached file gives the following error:

member-templates.cpp: In member function `std::string B<T>::toString() const':
member-templates.cpp:40: parse error before `;' token

SGI's MIPSpro Compilers 7.3.1.3m compiles the code fine, and so does MS Visual C++ 7 (!). Most of the time, GCC 3.x blows both of these out of the water, and thus my surprise when I got the above error. I couldn't find a PR relating to this, so I am wondering if this is this a known problem.

-Patrick


--
Patrick L. Hartling | Research Assistant, VRAC
patrick@137.org | 2274 Howe Hall Room 2624
PGP: http://www.137.org/patrick/pgp.txt | T: +1.515.294.4916
http://www.137.org/patrick/ | http://www.vrac.iastate.edu/
#include <iostream>
#include <string>
#include <sstream>

class VariantType
{
public:
   template<typename T>
   void setValue(const T& value)
   {
      std::ostringstream oss;
      oss << value;
      mData = oss.str();
   }

   template<typename T>
   T getValue() const
   {
      T t;
      std::istringstream iss(mData);
      iss >> t;
      return t;
   }

private:
   std::string mData;
};

template<typename T>
class B
{
public:
   B(const T& value)
   {
      mObj.setValue(value);
   }
   
   std::string toString() const
   {
      return mObj.getValue<std::string>();
   }

private:
   VariantType mObj;
};

int main()
{
   B<int> obj1(5);
   B<std::string> obj2("Hello");

   std::cout << "obj1: " << obj1.toString() << std::endl;
   std::cout << "obj2: " << obj2.toString() << std::endl;

   return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]