14

Here's a simple example:

class bar {};

template <typename>
class foo {};

template <>
using foo<int> = bar;

Is this allowed?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • 2
    How is this different from `typedef foo bar;`? – Kerrek SB Oct 18 '11 at 00:44
  • 3
    @KerrekSB: It's the complete opposite. This would be more like `typedef bar foo;` (though this is not allowed). In my example, `bar` already exists as a type, and `foo` becomes a new name for it. (What you wrote is equivalent to `using bar = foo;`, if that helps.) – R. Martinho Fernandes Oct 18 '11 at 00:46
  • Oh, I see. But then you might as well ask for *any* class definition to be copyable: `class Foo = Bar;` etc. I don't suppose that's possible... – Kerrek SB Oct 18 '11 at 00:48
  • `using Foo = Bar;`? (Or `typedef Bar Foo;`, if you prefer the old style.) I don't expect my example (if valid) to create a distinct type (please let me know I'm misunderstanding you). – R. Martinho Fernandes Oct 18 '11 at 00:50
  • Hmm, reading this answer: http://stackoverflow.com/questions/6622452/alias-template-specialisation/6623089#6623089 makes me believe this is indeed not possible. – R. Martinho Fernandes Oct 18 '11 at 00:54
  • Interesting. I was thinking about *copying* the type definition, not just aliasing, since I imagine that you'd have to provide (the equivalent of) a full class definition for an explicit specialization. The linked answer does indeed suggest that aliases are not the right tool for the job. – Kerrek SB Oct 18 '11 at 01:13
  • I wrote a few `define`s that generalize on Aotium's answer, here http://stackoverflow.com/a/14050604/225186 – alfC Dec 27 '12 at 06:57
  • There is slightly indirect way. see [Section 1.4](http://www.stroustrup.com/template-aliases.pdf) –  Dec 16 '14 at 15:09

3 Answers3

11
$ clang++ -std=c++0x test.cpp
test.cpp:6:1: error: explicit specialization of alias templates is not permitted
template <>
^~~~~~~~~~~
1 error generated.

Reference: 14.1 [temp.decls]/p3:

3 Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
11

Although direct specialization of the alias is impossible, here is a workaround. (I know this is an old post but it's a useful one.)

You can create a template struct with a typedef member, and specialize the struct. You can then create an alias that refers to the typedef member.

template <typename T>
struct foobase {};

template <typename T>
struct footype
  { typedef foobase<T> type; };

struct bar {};

template <>
struct footype<int>
  { typedef bar type; };

template <typename T>
using foo = typename footype<T>::type;

foo<int> x; // x is a bar.

This lets you specialize foo indirectly by specializing footype.

You could even tidy it up further by inheriting from a remote class that automatically provides the typedef. However, some may find this more of a hassle. Personally, I like it.

template <typename T>
struct remote
  { typedef T type; };

template <>
struct footype<float> :
  remote<bar> {};

foo<float> y; // y is a bar.
iPherian
  • 908
  • 15
  • 38
Aotium
  • 141
  • 1
  • 4
  • I have been using this trick, but it does help to have it out here. So, thanks for putting it up. And welcome to StackOverflow! – R. Martinho Fernandes Mar 19 '12 at 12:02
  • Just after writing a long answer with workaround for this limitation, I found your answer that uses the same principle. In any case I wrote in addition a couple of `define`s to make the code shorter. Take a look if you are interested: http://stackoverflow.com/a/14050604/225186 – alfC Dec 27 '12 at 06:55
  • 1
    I think you missed a `` in the `using` statement: `template using foo = typename footype::type;` – knedlsepp Mar 08 '16 at 12:55
7

According to §14.7.3/1 of the standard (also referred to in this other answer), aliases are not allowed as explicit specializations :(

An explicit specialization of any of the following:

  • function template
  • class template
  • member function of a class template
  • static data member of a class template
  • member class of a class template
  • member class template of a class or class template
  • member function template of a class or class template

can be declared[...]

Community
  • 1
  • 1
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510