views:

360

answers:

9

which programming languages support templates like C++?

A: 

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.

AProgrammer
What is the 'most recent one'? And surely saying that you don't know the answer to a question is a comment, not an answer, unless you also claim to know every programming language there is?
Pete Kirkham
@Pete Kirkham: However, "I don't know of a computer language that...." is a valid answer, assuming one is familiar with a large range of them (although the lack of reference to Common Lisp macros or D suggests a smaller range than I'd like). We're not going to establish a negative across every programming language in existence no matter what we do, so if the answer is "none" the closest we're going to get is "none any of us we can think of".
David Thornley
@David, the lack of reference to CL macro (or to Forth ;immediate if you want mention of another language) is voluntary. These features are base on different principles that template in C++ even if their use cases have an overlap with the one of template metaprogramming. About D, I plead guilty of having forgotten it. Another language I forgot is Aldor. If you are interested about computation on types, you should have a look at it.
AProgrammer
@AProgrammer: We may be getting off-topic here, but I'd think that CL macros and C++ templates are very similar mechanisms: ways to specify how to write code that will be compiled. I haven't used Forth in a long time, so I don't remember `;immediate`, but clearly anybody who wanted a similar facility could write it easily enough. (Forth is fun; I should get back to it sometime.)
David Thornley
@David, I wonder if you aren't looking at the result when I'm looking at the method. IMHO Lisp macros are about as similar to C++ templates as C is similar to Prolog. There are goals you can achieve both ways (and in their respective languages they are the preferred methods of achieving these goals), but the underlying principles and the interactions with the rest of the language are totally different. (I thing we are really on the original topic, but SO format isn't really suited to discussion)
AProgrammer
@AProgrammer: The method is to have a Turing-complete code generator embedded in the language. The big Lisp advantage here is that it's the target language, while C++ templates are a very arcane and sometimes awkward language. I think the big differences are in usage, partly because Lisp macros are easier to use than C++ template metaprogramming, and partly because Lisp macros have been around a lot longer. Give C++ templates more time and some improved syntax, nothing more basic, and they'll look a lot more like Lisp macros.
David Thornley
@David, It's not just syntaxic. Lisp macro will continue to be imperative, templates will continue be inference driven. Just like computations in C and Prolog. It is two different computation models which are at stake (at least until we get monad in C++ templates). There are other differences: Lisp and Lisp macros are dynamically typed, static typing and static type inference is the kernel of C++ templates. And I also think that the unification of the language with the macro/meta language is not only an advantage, it's a fundamental characteristic (see also the tower of meta levels in scheme).
AProgrammer
+1  A: 

Java and C# both have generics like C++, but they aren't implemented the same way.

duffymo
They're not only implemented differenty, they also are quite different. For one thing they only allow types as "template arguments" and for another they don't allow for computations.
sepp2k
Can you explain "don't allow for computations"? It's been a while since I've delved into C++ templates.
duffymo
@duffymo: As an example in C++ you can define a template `Fac`, so that for any non-negative integer `n` `Fac<n>` will be a class which contains one static int constant which holds the factorial of n. Even if you don't allow `int`s as template arguments, you could still encode the number as a class (e.g. `Fac<Succ<Succ<Succ<Zero> > > > >::value` would be 24). In java you cannot do that or any other sort of compile-time calculation.
sepp2k
generics are worked out at runtime, meaning there is a perf hit, while templates are compile-time, so generally you get faster code. generics generally put the burden of work on the writer while templates put it on the consumer - at least until we get concepts :-).
Kate Gregory
@duffymo: Here's the factorial code I was talking about: http://codepad.org/sjhzK2sr
sepp2k
Thank you for the kind explanations. I haven't written C++ in 8 years, and I never used templates that way. I stuck with STL, which from my point of view looked a lot like generics. The compile time reminder is appropriate, too.
duffymo
See http://stackoverflow.com/questions/3071450/improving-methods-with-variable-parameters-using-net-generics for some limitation of .NET (C#) generics.
sbi
+8  A: 

D has it

Johannes Schaub - litb
That's beyond sick (in a good way).
Alex B
+12  A: 

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.

sepp2k
+4  A: 

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.

Pete Kirkham
+6  A: 

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.

jalf
Common Lisp macros are evaluated at compile time, IIRC, which means that they leave executable code in their wake, just like C++ templates. Both CL and C++ have Turing-complete languages that create code to be compiled. CL differs in that its language is simply CL, while C++'s language is more arcane.
David Thornley
A: 

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.

PauliL
+3  A: 

D supports templates that are very similar to C++ templates, supporting IFTI, specialization, etc., but with some nice features added:

  1. Variadic templates.
  2. Templates parametrizable with floating point numbers and strings.
  3. 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.
  4. auto return, where the compiler deduces a templated function's return type.
  5. 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.
  6. Constraints, which are similar to C++0x concepts.
dsimcha
+1  A: 

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.

DeadMG