is std :: vector< T>一个'用户定义的类型'? [英] Is std::vector<T> a `user-defined type`?

查看:141
本文介绍了is std :: vector< T>一个'用户定义的类型'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前草案的17.6.4.2.1 / 1和17.6.4.2.1 / 2中标准限制了用户在命名空间std 中注入的特殊化。


如果C
++
程序在命名空间中添加了声明或定义,那么它的行为是未定义的
std

命名空间中的命名空间
std
,除非另有规定。程序可以将任何标准库模板的模板专用化
添加到命名空间
std
中,只要声明依赖于用户定义的类型
,专业化满足原始模板的标准库要求,并且没有明确禁止




我听说声称的一个选项是不是 std :: is_fundamental 是用户定义的类型,在这种情况下 std :: vector< int>



另一种替代方法是,用户定义的类型是一种类型用户定义。由于用户没有定义 std :: vector< int> std :: vector< int> 对用户定义的任何类型, std :: vector< int> 不是用户定义的类型



这个影响的一个实际问题是你可以为 std :: hash 注入 std :: tuple into namespace std ?能够这样做有点方便 - 替代方法是创建另一个命名空间,我们的 std :: tuple (以及 std 中的其他类型的哈希值不包含 hash 支持),当且仅当我们未能在该命名空间中找到一个哈希时,我们还会使用 std

$但是,如果这是合法的,那么当标准为 std ::添加 hash 专用化时, tuple 命名空间std ,专门的代码已经被打破,创建一个原因,以便将来不添加这样的专业化。

虽然我在谈论 std :: vector< int> 作为一个具体的例子, std 始终用户定义的类型。第二个问题是,即使不是,当用户使用时, std :: tuple< int> 变成用户定义类型变得滑溜:如果 std 中定义 std :: tuple< int> hash for std :: tuple< Ts ...> )。






目前有一个打开缺陷

解决方案

Stroustrup非常清楚,任何不是内置的类型是用户定义。使用C ++的编程原理和实践第9.1节第二段。



他甚至特别调用标准库类型作为用户定义类型的例子。换句话说,用户定义的类型是任何复合类型。



来源



文章明确提到并不是每个人都同意,但这是IMHO大多是一厢情愿的想法,而不是标准(和Stroustrup教授)实际上说,只有一些人想要读入它。


In 17.6.4.2.1/1 and 17.6.4.2.1/2 of the current draft standard restrictions are placed on specializations injected by users into namespace std.

The behavior of a C ++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

I cannot find where in the standard the phrase user-defined type is defined.

One option I have heard claimed is that a type that is not std::is_fundamental is a user-defined type, in which case std::vector<int> would be a user-defined type.

An alternative answer would be that a user-defined type is a type that a user defines. As users do not define std::vector<int>, and std::vector<int> is not dependent on any type a user defines, std::vector<int> is not a user-defined type.

A practical problem this impacts is "can you inject a specialization for std::hash for std::tuple<Ts...> into namespace std? Being able to do so is somewhat convenient -- the alternative is to create another namespace where we recursively build our hash for std::tuple (and possibly other types in std that do not have hash support), and if and only if we fail to find a hash in that namespace do we fall back on std.

However, if this is legal, then if and when the standard adds a hash specialization for std::tuple to namespace std, code that specialized it already would be broken, creating a reason not to add such specializations in the future.

While I am talking about std::vector<int> as a concrete example, I am trying to ask if types defined in std are ever user-defined type s. A secondary question is, even if not, maybe std::tuple<int> becomes a user-defined type when used by a user (this gets slippery: what then happens if something inside std defines std::tuple<int>, and you partial-specialize hash for std::tuple<Ts...>).


There is currently an open defect on this problem.

解决方案

Prof. Stroustrup is very clear that any type that is not built-in is user-defined. See the second paragraph of section 9.1 in Programming Principles and Practice Using C++.

He even specifically calls out "standard library types" as an example of user-defined types. In other words, a user-defined type is any compound type.

Source

The article explicitly mentions that not everyone seems to agree, but this is IMHO mostly wishful thinking and not what the standard (and Prof. Stroustrup) are actually saying, only what some people want to read into it.

这篇关于is std :: vector&lt; T&gt;一个'用户定义的类型'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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