std :: add_const的用例和类似 [英] Use cases for std::add_const and similar

查看:533
本文介绍了std :: add_const的用例和类似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< type_traits> 中的一些类型转换也可以使用核心语言语法来表示(例如 std :: add_const< T& is /等效于 const T )。 Dtto for std :: add_lvalue_reference ,以及其他人。这些类型特征的用途是什么?

Some type transformations in <type_traits> can also be expressed using core language syntax (e.g. std::add_const<T>::type is/seems equivalent to const T). Dtto for std::add_lvalue_reference, and perhaps others. What is the use for these type traits?

我完全理解标准将提供一个不完整的工具箱没有他们,我可以想象使用元方式,类似这样:

I fully understand the standard would be providing an "incomplete toolbox" without them, and I can imagine use in a meta way, something like this:

template<typename In, template <typename> class Modifier>
struct Apply {
  typedef typename Modifier<T>::type Out;
};

Apply<int, std::add_const>

这些特征还有其他用法,可以用语法表达,从完整性的意义和偶尔的元使用?

Are there any other use cases for these traits which can be expressed syntactically, or are they just included "out of a sense of completeness" and for the occasional meta-use?

推荐答案

这些特质来自Boost和添加他们的标准, N1345 ,引用Andrei Alexandrescu的话:

Those traits come from Boost and the proposal to add them to the standard, N1345, quotes Andrei Alexandrescu as saying:


我理解添加 add_const add_volatile add_cv add_pointer

同样的提案也给出了这个理由:

The same proposal also gives this rationale:


作者注:表面上add_const,add_volatile和add_cv类是不相关的,因为例如add_const :: type与T const相同,对于所有T (目前这不适用于函数类型 - 但问题295 地址这个)。然而,boost的经验是,有几个用户要求这些模板出现在库中,原因如下:(a)一些用户发现这些更明确 - 当特别组合变换模板时,用户喜欢那种文档。 (b)并非所有用户都知道cv限定引用是允许的并且没有效果,或者cv限定已经cv限定的类型是允许的并且没有效果。 (c)当cv限定作为引用的类型或已经具有cv限定符时,编译器可能发出警告,可以实现这些模板,以便在这些情况下抑制这些消息。

Author's Note: superficially the add_const, add_volatile and add_cv classes are irrelevant, since, for example, add_const::type is the same as T const, for all T (currently this does not apply to function types - but issue 295 addresses this). However the experience from boost is that several users have asked for these templates to be present in the library for the following reasons: (a) Some users find these more explicit - when combining transformation templates in particular, users like the kind of "built in documentation" that these templates provide. (b) Not all users are aware that cv-qualifying a reference is allowed and has no effect, or that cv-qualifying a type that is already cv-qualified is allowed and has no effect. (c) Compilers may emit warnings when cv-qualifying a type that is a reference, or already has a cv-qualifier, these templates can be implemented such that these messages are suppressed in these cases.

此外,对于 add_reference (重命名为 add_lvalue_reference 标准):

Also, for add_reference (renamed to add_lvalue_reference in the standard):


作者注:add_reference模板是boost类型traits库背后的原动机之一。但是,问题106 的解决方案会使模板显示为冗余。尽管add_reference在无意中创建对模板代码中的引用的引用时可以有效地抑制编译器警告。

Author's Note: the add_reference template was one of the original motivations behind the boost type traits library. However the resolution to issue 106 makes the template appear largely redundant. In spite of that add_reference may well be useful in suppressing compiler warnings when inadvertently creating references to references in template code.

这篇关于std :: add_const的用例和类似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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