在 current draft standard 的 17.6.4.2.1/1 和 17.6.4.2.1/2 中对用户注入(inject) 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.
我找不到在标准中定义短语用户定义类型的位置。
我听说的一个选项是不是 std::is_fundamental
的类型。是用户定义的类型,在这种情况下是 std::vector<int>
将是一个用户定义的类型。
另一种答案是用户定义的类型是用户定义的类型。由于用户未定义std::vector<int>
, 和 std::vector<int>
不依赖于用户定义的任何类型,std::vector<int>
不是用户定义的类型。
这种影响的一个实际问题是“您能否将 std::hash
的专用化 std::tuple<Ts...>
注入(inject)到 namespace std
中?这样做有点方便——另一种方法是创建另一个命名空间,在其中递归地构建我们的std::tuple
的散列(可能还有 std
中不支持 hash
的其他类型),并且当且仅当我们无法在该命名空间中找到散列时,我们才使用 std
。
但是,如果这是合法的,那么如果标准添加了 hash
std::tuple
的特化至namespace std
, 已经专门化它的代码将被破坏,从而有理由在将来不添加此类专门化。
虽然我在谈论 std::vector<int>
作为一个具体的例子,我试图询问 std
中定义的类型是否永远是用户定义的类型。第二个问题是,即使不是,也许 std::tuple<int>
当用户使用时变成用户定义的类型(这会变得很棘手:如果 std
中的某些内容定义了 std::tuple<int>
并且您将 hash
部分特化为 std::tuple<Ts...>
会发生什么) .
目前有一个 open defect关于这个问题。
最佳答案
教授。 Stroustrup 非常清楚任何不是内置的类型都是用户定义的。请参阅《C++ 编程原理与实践》中 9.1 节的第二段。
他甚至专门将“标准库类型”称为用户定义类型的示例。换句话说,用户定义的类型是任何复合类型。
Source
文章明确提到似乎并非所有人都同意,但恕我直言,这主要是一厢情愿的想法,而不是标准(和 Stroustrup 教授)实际所说的,只是某些人想要读入的内容。
关于c++ - std::vector<T> 是 `user-defined type` 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23659222/