Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some other languages that support "partial specialization"?

Partial template specialization is one of the most important concepts for generic programming in C++. For example: to implement a generic swap function:

template <typename T>
void swap(T &x, T &y) {
  const T tmp = x;
  y = x;
  x = tmp;
}

To specialize it for a vector to support O(1) swap:

template <typename T, class Alloc>
void swap(vector<T, Alloc> &x, vector<T, Alloc> &y) { x.swap(y); }

So you can always get optimal performance when you call swap(x, y) in a generic function;

Much appreciated, if you can post the equivalent (or the canonical example of partial specialization of the language if the language doesn't support the swap concept) in alternative languages.

EDIT: so it looks like many people who answered/commented really don't known what partial specialization is, and that the generic swap example seems to get in the way of understanding by some people. A more general example would be:

template <typename T>
void foo(T x) { generic_foo(x); }

A partial specialization would be:

template <typename T>
void foo(vector<T> x) { partially_specialized_algo_for_vector(x); }

A complete specialization would be:

void foo(vector<bool> bitmap) { special_algo_for_bitmap(bitmap); }

Why this is important? because you can call foo(anything) in a generic function:

template <typename T>
void bar(T x) {
  // stuff...
  foo(x);
  // more stuff...
}

and get the most appropriate implementation at compile time. This is one way for C++ to achieve abstraction w/ minimal performance penalty.

Hope it helps clearing up the concept of "partial specialization". In a way, this is how C++ do type pattern matching without needing the explicit pattern matching syntax (say the match keyword in Ocaml/F#), which sometimes gets in the way for generic programming.

like image 399
obecalp Avatar asked Dec 18 '08 07:12

obecalp


People also ask

What is template specialization in c++?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is explicit Specialisation?

Explicit (full) specializationAllows customizing the template code for a given set of template arguments.


1 Answers

D supports partial specialization:

  • Language overview
  • Template feature comparison (with C++ 98 and 0x).

(scan for "partial" in the above links).

The second link in particular will give you a very detailed breakdown of what you can do with template specialization, not only in D but in C++ as well.

Here's a D specific example of swap. It should print out the message for the swap specialized for the Thing class.

import std.stdio;    // for writefln

// Class with swap method

class Thing(T)
{
public:

    this(T thing)
    {
        this.thing = thing;
    }

    // Implementation is the same as generic swap, but it will be called instead.
    void swap(Thing that)
    {
       const T tmp = this.thing;
       this.thing = that.thing;
       that.thing = tmp;
    }

public:

    T thing;
}


// Swap generic function

void swap(T)(ref T lhs, ref T rhs)
{
    writefln("Generic swap.");

    const T tmp = lhs;
    lhs = rhs;
    rhs = tmp;
}

void swap(T : Thing!(U))(ref T lhs, ref T rhs)
{
    writefln("Specialized swap method for Things.");

    lhs.swap(rhs);
}

// Test case

int main()
{
    auto v1 = new Thing!(int)(10);
    auto v2 = new Thing!(int)(20);

    assert (v1.thing == 10);
    assert (v2.thing == 20);
    swap(v1, v2);
    assert (v1.thing == 20);
    assert (v2.thing == 10);

    return 0;
}
like image 83
quark Avatar answered Oct 18 '22 14:10

quark