Adib
Adib

Reputation: 723

if template alias declaration resolve to a new family of types?

considering following code :

what happens when we use a template alias, as in X? Is the specialization X still used, since Z is an alias for Y? what happens when we use a template alias , if template alias declaration resolve to a new family of types ?

#include <iostream>

template <template <typename> class>
struct X {
  X() { std::cout << "1"; }
};

template <typename>
struct Y {};

template <typename T>
using Z = Y<T>;

template <>
struct X<Y> {
  X() { std::cout << "2"; }
};

int main() {
  X<Y> x1;
  X<Z> x2;
}

output :21

Upvotes: 1

Views: 109

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93324

An alias is merely an alias, and does not introduce a new family of types. In fact, running your code prints 22 - this is because Zis an alias for Y, therefore the specialization is getting hit twice.

Unfortunately, while g++ prints 22 as expected, clang prints 21. I suspect it's due to bug #26093. In fact, converting the template templates to normal templates prevents the bug from occurring:

template <typename>
struct X {
    X() { std::cout << "1"; }
};

struct Y {};
using Z = Y;

template <>
struct X<Y> {
    X() { std::cout << "2"; }
};

int main() {
    X<Y> x1;
    X<Z> x2;
}

wandbox example

Upvotes: 3

Related Questions