Portability of C ++ Templates

I am updating the 10 year old code base and have used the Metrowerks Code Warrior on Mac and Windows.

I am upgrading to OS X, Xcode 3.2, Universal Binary.

It seems that I am getting a lot of mistakes related to patterns, and I am not a genius on patterns (and forget to eat a healthy dose of frozen patterns for breakfast), I am wondering about the problems of portability of patterns.

IIRC, patterns are / or may be compiler specific?

Does anyone have a tip or tutorial on patterns they recommend?

+4
source share
4 answers

Yes and no - the most reasonable template code written for one current compiler works fine on other current compilers. Compilers progressed over time, so many new codes will not work on older compilers, and vice versa. The biggest culprit of old code on new compilers requires "typename" in quite a few places where old compilers will accept code without it.

The most common problem is that the code looks something like this:

template <class T> class XYZ { T::ya; }; 

Most older compilers (incorrectly) interpret "T :: y" as a type, but in the template it is actually impossible to be sure of this, because T can be any type. To make the code work with a modern (more accurate) compiler, you need to change it to: typename T::ya; so that the compiler knows that T::y is a type name.

+5
source

The templates themselves have well-defined behavior, as defined in Β§14 of the standard.

What depends on the implementation is the limits of the use of the template. For example, from Appendix B (which lists recommended minimum limits):

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

If you are more dependent on behavior than this, it may be implementation dependent. It should be noted that the compiler should not provide these minimum restrictions for compliance with standards.

If you post some actual codes / errors, we can tell you why you are getting an error message. Most likely, you are old code used by some extensions specific to the compiler, or otherwise allowed you to use explicitly forbidden behavior.

+1
source

Compiler capabilities have improved significantly in ten years.
I would question the standards compiler and STL 10 years ago. I believe that the standard was just introduced ten years ago, and it requires compilers to catch up with the standard.

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

+1
source

In 2000, there was not a single compiler that implemented all aspects of standard templates. I would dare say that no one even understood that it was possible with templates before Alexandrescu released its Modern C ++ design in 2001.

However, Metrowerks was one of the best. If it compiles on version 7 or later, it should be very possible to get it working on a modern, more standard, compiler pretty quickly.

If I remember correctly, the biggest problem with the Metrowerks compilers in the early days was that wherever typename appeared, the next one was simply interpreted as a type, regardless of what followed.

This meant that you could do it, and I saw completely non-standard things with this, such as forwarding typedef declarations.

Another part of the templates that made them wait a long time is all the wrt template template parameters and default template arguments.

Put some specific errors, if you can't get it to work, they probably all fall into one or two β€œclasses” of problems, and someone can quickly help you.

As I said, Metrowerks had a pretty good C ++ compiler, especially its STL implementation, mainly thanks to Howard Hinnant .

0
source

Source: https://habr.com/ru/post/1300363/


All Articles