`typedef typename Foo<T>::Bar Bar' 的模板声明 [英] template declaration of `typedef typename Foo<T>::Bar Bar'

查看:45
本文介绍了`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

这篇关于`typedef typename Foo&lt;T&gt;::Bar Bar' 的模板声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆