为什么C ++ 11没有模板typedef? [英] Why does C++11 not have template typedef?

查看:92
本文介绍了为什么C ++ 11没有模板typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++ 11没有模板typedef",如

Why does C++11 not have "template typedefs", like

template<typename T> typedef std::vector<T, myalloc<T>> vec;

相反,它们只允许使用新语法:

Instead they only allow the new syntax:

template<typename T> using vec = std::vector<T, myalloc<T>>;

推荐答案

n1406 是Herb Sutter提出的"typedef模板"提案,该提案模仿了您问题中的语法. n1499 ,其中提出了模板别名"取代它,它包含C ++ 11中当前存在的using语法.

n1406 was the proposal by Herb Sutter for "typedef templates" which mimics the syntax in your question. n1499 which proposes "template aliases" supersedes it, which contains the using syntax that's currently present in C++11.

这两篇文章都解决了"typedef模板"的主要缺点之一.从n1406开始:

One of the main drawbacks of the "typedef templates" is addressed in both papers. From n1406:

在现有实践中,包括在标准库中,键入名称 嵌套在帮助器类模板中的模板可用于解决此问题 在很多情况下都是问题.以下是这种平常的例子 解决方法;主要缺点是在使用时需要编写:: Type typedef的名称.

In existing practice, including in the standard library, type names nested inside helper class templates are used to work around this problem in many cases. The following is one example of this usual workaround; the main drawback is the need to write ::Type when using the typedef’d name.

template< typename T >
struct SharedPtr
{
  typedef Loki::SmartPtr
    <
      T,                // note, T still varies
      RefCounted,       // but everything else is fixed
      NoChecking,
      false,
      PointsToOneObject,
      SingleThreaded,
      SimplePointer<T>  // note, T can be used as here
    >
  Type;
};

SharedPtr<int>::Type p; // sample usage, "::Type" is ugly

我们真正想要做的就是这个:

What we’d really like to be able to do is simply this:

template< typename T >
typedef Loki::SmartPtr
  <
    T,                // note, T still varies
    RefCounted,       // but everything else is fixed
    NoChecking,
    false,
    PointsToOneObject,
    SingleThreaded,
    SimplePointer<T>  // note, T can be used as here
  >
  SharedPtr;

SharedPtr<int> p;     // sample usage, "::Type" is ugly

[...]

解决方法很丑,最好将其替换为 一流的语言支持,可为用户提供自然的C ++模板 语法.

The workaround is ugly, and it would be good to replace it with first-class language support that offers users a natural C++ template syntax.

一流的语言支持"以模板别名的形式出现.现在,我们可以看看n1499怎么说:

That "first-class language support" comes in the form of template aliases. We can now look at what n1499 has to say:

在本文中,我们将重点介绍一个别名机制,该别名机制 允许N1406中提到的两种语义共存而不是 被视为互斥的.首先让我们考虑一个玩具示例:

In this paper we will focus on describing an aliasing mechanism that allows the two semantics mentioned in N1406 to coexist instead being regarded as mutually exclusive. First let’s consider a toy example:

template <typename T>
class MyAlloc {/*...*/};

template <typename T, class A>
class MyVector {/*...*/};

template <typename T>
struct Vec {
typedef MyVector<T, MyAlloc<T> > type;
};

Vec<int>::type p; // sample usage

这个习语的基本问题和主要动机 对于这个建议,是成语使模板参数 出现在不可推论的上下文中.也就是说,将不可能 调用下面的函数foo而不显式指定模板 争论.

The fundamental problem with this idiom, and the main motivating fact for this proposal, is that the idiom causes the template parameters to appear in non-deducible context. That is, it will not be possible to call the function foo below without explicitly specifying template arguments.

template <typename T> void foo (Vec<T>::type&);

此外,语法有些丑陋.我们宁愿避免嵌套 :: type调用.我们更喜欢以下内容:

Also, the syntax is somewhat ugly. We would rather avoid the nested ::type call. We’d prefer something like the following:

template <typename T>
using Vec = MyVector<T, MyAlloc<T> >; //defined in section 2 below

Vec<int> p;      // sample usage

请注意,我们特别避免使用"typedef template"一词, 引入涉及"using"和"="对的新语法以提供帮助 避免混淆:我们在这里没有定义任何类型,我们是 为 type-id 的抽象引入同义词(即别名) (即类型表达式)涉及模板参数.如果模板 在类型表达式的可推导上下文中使用参数,然后 每当模板别名用于形成 template-id 时, 可以推导出相应模板参数的值–更多 在此之后.无论如何,现在都可以编写泛型 在可推论的上下文中对Vec<T>进行操作的函数,以及 语法也得到了改善.例如,我们可以将foo重写为:

Note that we specifically avoid the term "typedef template" and introduce the new syntax involving the pair "using" and "=" to help avoid confusion: we are not defining any types here, we are introducing a synonym (i.e. alias) for an abstraction of a type-id (i.e. type expression) involving template parameters. If the template parameters are used in deducible contexts in the type expression then whenever the template alias is used to form a template-id, the values of the corresponding template parameters can be deduced – more on this will follow. In any case, it is now possible to write generic functions which operate on Vec<T> in deducible context, and the syntax is improved as well. For example we could rewrite foo as:

template <typename T> void foo (Vec<T>&);

我们在此强调提出提议的主要原因之一 模板别名是这样,以便推导参数并调用 foo(p)将成功.

We underscore here that one of the primary reasons for proposing template aliases was so that argument deduction and the call to foo(p) will succeed.

因此,您可以看到n1499解决了n1406中的问题,并且引入了一种更简洁,更易于阅读的语法.

So you can see that n1499 addresses the problems in n1406 as well as introducing a syntax that's much cleaner and easier to read.

这篇关于为什么C ++ 11没有模板typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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