`typedef typename Foo<T>::Bar Bar' 的模板声明
问题描述:
我在声明如下所示的模板化类型时遇到了很大的困难.
I am encountering great difficulty in declaring a templated type as shown below.
#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.
I am doing this because I want avoid writing typename Foo::Bar throught my code.
我做错了什么?
答
C++ 中的 typedef
声明不能是模板.但是,C++11 添加了使用 using
声明的替代语法以允许参数化类型别名:
The typedef
declaration in C++ cannot be a template. However, C++11 added an alternative syntax using the using
declaration to allow parametrized type aliases:
template <typename T>
using Bar = typename Foo<T>::Bar;
现在您可以使用:
Bar<int> x; // is a Foo<int>::Bar