iOSPorter
iOSPorter

Reputation: 3

GCC template errors

I'm trying to port a C++ project to iOS. It compiles just fine in QtCreator on Linux and Windows and also on MSVC. Now on Xcode/GCC, with a certain templated class I get the following error: "error: too few template-parameter-lists".

The code that causes this error looks something like this:

template <typename TA, typename TB, int Type>
class MyClassImpl
{
public:
   MyClassImpl();
   virtual int GetType() const
   {
       return type;
   }
};    

typedef MyClassImpl<float, int, 12> MyFloatIntClass;

MyFloatIntClass::MyFloatIntClass()
{
...
}

int MyFloatIntClass::GetType() const
{
   return 22;
}

I'm guessing that something about the typedef syntax is illegal and GCC is more strict on the standard. Can anybody tell me what exactly the problem is and how I can fix it?

Upvotes: 0

Views: 470

Answers (2)

Robert Mason
Robert Mason

Reputation: 4039

This is just a guess, but do you need to add template<>?

Since it's a template specialization, I believe it still needs to be a template.

ex.

template<>
MyFloatIntClass::MyClassImpl() {}

template<>
int MyFloatIntClass::GetType() const {
    return 22;
}

EDIT: From modelnine's answer- turns out it needs the untypedef'd name for the ctor.

EDIT2: The following code works fine for me:

template <typename TA, typename TB, int Type>
class MyClassImpl
{
public:
   MyClassImpl();
   MyClassImpl(const MyClassImpl<TA, TB, Type>&);
   virtual int GetType() const
   {
       return Type;
   }
   const MyClassImpl<TA, TB, Type>& operator=(const MyClassImpl<TA, TB, Type>&);
};    

typedef MyClassImpl<float, int, 12> MyFloatIntClass;

template<>
MyFloatIntClass::MyClassImpl()
{
 //
}

template<>
MyFloatIntClass::MyClassImpl( const MyFloatIntClass& rhs )
{
 //
}

template<>
const MyFloatIntClass& MyFloatIntClass::operator=( const MyFloatIntClass& rhs )
{
  return *this;
}

template<>
int MyFloatIntClass::GetType() const
{
   return 22;
}

Upvotes: 3

modelnine
modelnine

Reputation: 1499

As you're defining full specializations of methods of the respective class, you'll still need to prefix the definitions with template <>, which is the "template-parameter-lists" you're missing. Furthermore, the constructor must be named as the class is, so MyFloatIntClass::MyFloatIntClass() is illegal (as MyFloatIntClass is just an alias, not a class name). The following compiles fine for me (g++ 4.5.3):

template <typename TA, typename TB, int Type>
class MyClassImpl
{
public:
   MyClassImpl();
   virtual int GetType() const
   {
       return Type;
   }
};    

typedef MyClassImpl<float, int, 12> MyFloatIntClass;

template <>
MyFloatIntClass::MyClassImpl()
{
}

template <>
int MyFloatIntClass::GetType() const
{
   return 22;
}

Upvotes: 4

Related Questions