这是一个简单的示例:
class bar {};
template <typename>
class foo {};
template <>
using foo<int> = bar;
这样做是允许的吗?
这是一个简单的示例:
class bar {};
template <typename>
class foo {};
template <>
using foo<int> = bar;
这样做是允许的吗?
虽然无法直接特化别名,但是有一个解决方法。(我知道这是一个旧帖子,但它很有用。)
您可以创建一个具有typedef成员的模板结构体,并对结构体进行特化。 然后,您可以创建一个引用typedef成员的别名。
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.
footype
来间接地专门化foo
。template <typename T>
struct remote
{ typedef T type; };
template <>
struct footype<float> :
remote<bar> {};
foo<float> y; // y is a bar.
define
来使代码更短。如果你感兴趣,请看一下:https://dev59.com/Smw15IYBdhLWcg3wbLLU#14050604 - alfCusing
语句中漏掉了一个<T>
:template <typename T> using foo = typename footype<T>::type;
- knedlsepp$ clang++ -std=c++0x test.cpp
test.cpp:6:1: error: explicit specialization of alias templates is not permitted
template <>
^~~~~~~~~~~
1 error generated.
参考资料: 14.1 [temp.decls]/p3:
3 因为别名声明不能声明一个模板ID,所以无法对别名模板进行部分或显式特化。
typedef foo<int> bar;
有何不同? - Kerrek SBtypedef bar foo<int>;
(虽然这是不被允许的)。在我的例子中,bar
已经存在作为一种类型,而foo<int>
成为它的一个新名称。(如果有帮助的话,您所写的相当于using bar = foo<int>;
。) - R. Martinho Fernandesclass Foo = Bar;
等等。我想这是不可能的... - Kerrek SBusing Foo = Bar;
?(或者如果你更喜欢旧的风格,可以使用typedef Bar Foo;
。)我不希望我的示例(如果有效)创建一个不同的类型(请让我知道是否我误解了你)。 - R. Martinho Fernandes