tags:

views:

91

answers:

4

I am updating a code base that is 10 years old and used Metrowerks Code Warrior on Mac and Windows.

I am updating to OS X, XCode 3.2, Universal Binary.

I seem to be getting a lot of template related errors and not being a genius on templates (and forgetting to eat a healthy dose of frosted templates for breakfast), I find myself wondering about template portability issues.

IIRC, templates are/or can be compiler specific?

Does anyone have advice or a tutorial on templates that they recommend?

+1  A: 

Templates themselves have well-defined behavior, as defined in §14 in the standard.

What is implementation-dependent is the limits of template use. For example, from Annex B (which lists recommended limit minimums):

  • Template arguments in a template declaration [1024].
  • Recursively nested template instantiations [17].

If you're depending on behavior more than these, it may be implementation dependent. It should be noted a compiler does not have to provide these minimal limits to remain standards compliant.

If you post some actual code/errors, we can tell you why you're getting an error. Likely, you're old code used some compiler-specific extensions or otherwise was allowed to use explicitly forbidden behavior.

GMan
Actually, those minimum limits were set before Boost, Loki, etcetera. Modern compilers significantly exceed the formal limits.
MSalters
+5  A: 

Yes and no -- most reasonable template code written for one current compiler will work fine on other current compilers. Compilers have progressed over time, so a lot of new code won't work on old compilers, and vice versa. The biggest culprit with old code on new compilers is needing "typename" in quite a few places that old compilers would accept the code without it.

The most common problem is with code something like this:

template <class T>
class XYZ { 
    T::y a;
};

Most older compilers would (incorrectly) interpret "T::y" as a type -- but in a template, it's actually impossible to be sure of that, because T might be any type. To make the code work with a modern (more accurate) compiler, you need to change that to: typename T::y a;, to let the compiler know that T::y is the name of a type.

Jerry Coffin
+1  A: 

The ability of compilers has improved a lot in ten years.
I would question the standards compliance of the compiler and the STL from 10 years ago. I belive tha standard had only just been introduced ten years ago and it takes compilers a while to catch up with the standard.

In modern compilers I think you will find that template code is relatively portable across compilers and the standards comitee is very carefull about changes to the standard to make sure it does not break compatabilty (very often).

Martin York
A: 

There wasn't a single compiler in 2000 which implemented all aspects of standard templates. I'd dare to say no one even realised what was possible with templates till Alexandrescu released his Modern C++ design in 2001.

That said, Metrowerks was one of the better ones. If it compiles on version 7 or later, it should be very feasible to get it working on a modern, more standard compliant, compiler rather fast.

If I remember correctly the biggest problem with Metrowerks compilers in the early days was that wherever typename appeared, what followed was simply interpreted as a type, no matter what followed it.

This meant you could do, and I've seen, completely non standard stuff with that, like forward declaring typedefs.

Another part of templates that took them pretty long to get right was everything w.r.t. template template parameters and default template arguments.

Post some specific errors if you can't get it to work, they'll probably all fall in one or two 'classes' of problems, and someone will quickly be able to help you.

As I already said, Metrowerks had a pretty good C++ compiler, especially their STL implementation, largely thanks to Howard Hinnant I think.

Pieter