fredrik.eriksson

Coffee and a keyboard

Template typedef

Ever wanted to to do a template typedef like this:

int main(int argc, char* argv[])
{
    template<class T>
    typedef std::map<int, T>    integer;

    integer; integer_string;
}

Then you would have noticed that this is not possible. But with a small class the functionality could easily be emulated:

template<template<class X, class Y> class T1, class T2>
struct templatetypedef
{
    template<class X>
    struct T
    {
        typedef T1<T2, X>       type;
    };
};

When using this class you would then simply:

int main(int argc, char* argv[])
{
    typedef templatetypedef<std::map, int> integer;

    integer::T<std::string>::type integer_string;
}

It would not cause any overhead because everything is done at compile time. The syntax on the other hand is not nearly as nice but is not terrible.