lundi 30 mai 2016

Why aren't template expressions simplified internally?

The following example doesn't work:

#include <iostream>

template <int index, template <typename> class Head,
        template <typename> class... Tail>
struct TemplateTupleElement {
    template <typename T>
    using Type =
            typename TemplateTupleElement<index - 1, Tail...>::template Type<T>;
};

template <template <typename> class Head, template <typename> class... Tail>
struct TemplateTupleElement<0, Head, Tail...> {
    template <typename T>
    using Type = Head<T>;
};

template <typename T>
class Dummy {
};

template <template <typename> class T>
class TemplateDummy {
public:
    static void print() {
        std::cout << "Template" << std::endl;
    }
};

template <>
class TemplateDummy<Dummy> {
public:
    static void print() {
        std::cout << "Specialization" << std::endl;
    }
};

int main(int argc, char* argv[]) {
    TemplateDummy<TemplateTupleElement<0, Dummy, Dummy>::Type>::print();
    TemplateDummy<TemplateTupleElement<1, Dummy, Dummy>::Type>::print();

    return 0;
}

The output is:

Specialization
Template

but I expected it to be:

Specialization
Specialization

I can understand that internally the compiler (g++ (Ubuntu 5.3.0-3ubuntu1~14.04) 5.3.0 20151204) represents the two template expressions differently:

  1. Dummy
  2. TemplateTupleElement<1, Dummy, Dummy>::Type

Why isn't the second expression "simplified" into the first expression?

Aucun commentaire:

Enregistrer un commentaire