Template (C++) - Generic Programming Features in Other Languages

Generic Programming Features in Other Languages

Initially, the concept of templates was not included in some languages, such as Java and C# 1.0. Java's adoption of generics mimics the behaviour of templates, but is technically different. C# added generics (parameterized types) in .NET 2.0. The generics in Ada predate C++ templates.

Although C++ templates, Java generics, and .NET generics are often considered similar, generics only mimic the basic behavior of C++ templates. Some of the advanced template features utilized by libraries such as Boost and STLSoft, and implementations of the STL itself, for template metaprogramming (explicit or partial specialization, default template arguments, template non-type arguments, template template arguments, ...) are not available with generics.

The D programming language attempts to build on C++ redesigning a better template system. A significant addition is the inclusion of the static if statement, which allows conditional compilation of code based on any information known at compile time. For example:

template factorial(ulong n) { static if( n <= 1 ) const factorial = 1; else const factorial = n * factorial!(n-1); };

D's CTFE (Compile time function execution) feature allows to do the same thing :

ulong factorial(ulong n) { if(n <= 1) return 1; else return n * factorial(n - 1); } void main { ulong foo = factorial(4); // known at run-time static foo2 = factorial(4); // known at compile-time }

Also note that the ! delimiters are used rather than the <> delimiters. This prevents ambiguity in the parsing of templates.

Other significant features include typesafe variadic template functions.

// Simple example, assumes all arguments are of the same type. T max(T...)(T args) { static assert(args.length > 1, "Insufficient arguments."); // T is the type of the first argument, // args is the first argument. T max = args; // Tuple can be iterated over and sliced like an array. foreach (arg; args) if (arg > max) max = arg; return max; }

This function will work for any number of arguments, with the foreach iteration over the tuple of arguments expanded at compile time.

D templates allow a simple form of Constraints too. They can be expressed as an arbitrarily complex predicate that must evaluate at compile time. If it's true the template is a match for the arguments, otherwise the template is ignored during overload matching.

template Foo(int N) if (N & 1) {...} // A template Foo(int N) if (!(N & 1)) {...} // B Foo!(3) // Instantiates A Foo!(64) // Instantiates B template Bar(T) if (isFloatingPoint!T) {...} Bar!(3.5) // Instantiates Bar Bar!(3) // Fails

Something similar can be done in C++ with Boost enable_if.

In C++ templates, the compile-time cases are performed by pattern matching over the template arguments, so the Factorial template's base case is implemented by matching 0 rather than with an inequality test, which is unavailable:

// Induction template struct Factorial { static const int value = N * Factorial::value; }; // Base case via template specialization: template <> struct Factorial<0> { static const int value = 1; };

With these definitions, one can compute, say 6! at compile time using the expression Factorial<6>::value.

Read more about this topic:  Template (C++)

Famous quotes containing the words generic, programming, features and/or languages:

    “Mother” has always been a generic term synonymous with love, devotion, and sacrifice. There’s always been something mystical and reverent about them. They’re the Walter Cronkites of the human race . . . infallible, virtuous, without flaws and conceived without original sin, with no room for ambivalence.
    Erma Bombeck (20th century)

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    “It looks as if
    Some pallid thing had squashed its features flat
    And its eyes shut with overeagerness
    To see what people found so interesting
    In one another, and had gone to sleep
    Of its own stupid lack of understanding,
    Or broken its white neck of mushroom stuff
    Short off, and died against the windowpane.”
    Robert Frost (1874–1963)

    I am always sorry when any language is lost, because languages are the pedigree of nations.
    Samuel Johnson (1709–1784)