which programming languages support templates like C++?
I know of no other programming languages which have template similar to the one of C++ (with explicit and partial specialization). Some languages (Ada, ML family, Eiffel, and most recent one) have a generic feature which cover some but not of the use of templates (for instance template metaprogramming is something not provided). I'd not be surprised if some experimental language covers the whole range in a more friendlier syntax, but I know of none.
Java and C# both have generics like C++, but they aren't implemented the same way.
D has templates which are very similar to C++ templates.
Lots of other languages (java, C#, haskell, ML, scala) have parametric polymorphism (aka generics) which allows for some of the use cases of C++ templates.
Also some languages have a preprocessor system which can be used for what template meta programming is used fo in C++ (e.g. template haskell).
Also some advanced type system features (like generalized algebraic data types in ghc haskell) allow you to express some of the things you can express in C++ with templates.
Wikipedia's template-metaprogramming article lists Curl, D, Eiffel, Haskell, ML and XL (which I'd not heard of before). Also Lisp macros are somewhat similar in that they offer full computation, but are part of the same language as the rest of the program, rather than being a separate language as C++ templates are.
Depends on what you mean by "like C++".
D has templates very like those of C++, but they have a different syntax. Is that "like C++"?
C# and Java have (very different implementations of) generics which have some of the same problems, and have a superficially similar syntax. Is that "like C++"?
A lot of functional programming languages (such as SML) have parametric polymorphism, which is a more general term for the same thing. Is that "like C++"?
LISP has macros which let you do practically anything, including anything templates can do. But it doesn't happen at compile-time, and the syntax is different. Is that "like C++"?
No other language has templates exactly like C++.
But a huge number of languages implement parametric polymorphism and many languages try to solve some of the same problems with different mechanisms.
RapidQ has templates similar to those of C++.
Here is an example code snippet:
TYPE Arrays<DataType, Size> EXTENDS QOBJECT
Item(Size) AS DataType
SUB Clear
DIM N AS DataType
DIM I AS LONG
DIM V AS VARIANT
WITH Arrays
V = .Item(0)
IF VARTYPE(V) = 2 THEN
'-- String data type'
FOR I = 0 TO Size
.Item(I) = ""
NEXT
ELSE
'-- Integer/Float'
FOR I = 0 TO Size
.Item(I) = 0
NEXT
END IF
END WITH
END SUB
END TYPE
DIM IntArray AS Arrays<INTEGER, 100>
DIM StrArray AS Arrays<STRING, 50>
IntArray.Clear
IntArray.Item(1) = 99
StrArray.Clear
StrArray.Item(1) = "Hello world"
RapidQ is a free semi-object oriented BASIC language especially created for rapid development of GUI applications for Windows, Unix and Linux. Unfortunately it's development ended short in the year 2000, less than a year after first release. But the compiler and IDE are still available for example from the files section of the RapidQ Yahoo group.
D supports templates that are very similar to C++ templates, supporting IFTI, specialization, etc., but with some nice features added:
- Variadic templates.
- Templates parametrizable with floating point numbers and strings.
static if
statements (kind of like#ifdef
except that it can be used with any value known at compile time, including values computed by templates.auto
return, where the compiler deduces a templated function's return type.- Compile time function evaluation: Technically this has nothing to do with templates, but it makes many use cases for templates easier. Basically, you can evaluate some functions consisting of normal D code at compile time and store the results in enums, etc.
- Constraints, which are similar to C++0x concepts.
D is by far and away the closest you'll get- it's the same as C++, but with a few extras. Generics are superficially similar but aren't that close when you get down to it.
The other question is, what other characteristics of a language are you wanting? For example, I could claim that you could use templates in any interpreted language, since you can evaluate code at runtime.