我在声明模板类型时遇到了很大的困难,如下所示。
#include <cstdlib>
#include <iostream>
using namespace std;
template <class T>
class Foo
{
typedef T Bar;
};
template <class T>
typedef typename Foo<T>::Bar Bar;
int main(int argc, char *argv[])
{
Bar bar;
Foo<int> foo;
system("PAUSE");
return EXIT_SUCCESS;
}
我收到错误
template declaration of `typedef typename Foo<T>::Bar Bar'
关于线路
template <class T>
typedef typename Foo<T>::Bar Bar;
我这样做是因为我想避免在我的代码中编写类型名 Foo::Bar 。
我究竟做错了什么?
The typedef
C++ 中的声明不能是模板。然而,C++11 添加了一种替代语法,使用using
允许参数化类型别名的声明:
template <typename T>
using Bar = typename Foo<T>::Bar;
现在您可以使用:
Bar<int> x; // is a Foo<int>::Bar
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)