什么是2D向量构造打破C ++ 11的变化? [英] What is 2D vector construction breaking change in C++11?

查看:101
本文介绍了什么是2D向量构造打破C ++ 11的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读一个问题的答案时在MS Connect网站上我注意到了答复的以下部分:

while reading answer to one question on MS Connect site I noticed the following part of the reply:


这是标准的几个突变之一我是
意识到的库(其他主要的是不可变集, 2D向量
构造
)。

答案可以被认为是合法的,具有高概率,因为它是来自实施STL的MS员工。

Answer can be considered legit with high probability since it is from MS employee that works on implementing STL.

那么任何人都知道他到底指的是什么。

So does anybody knows what exactly he refers to?

推荐答案

我给斯蒂芬发电子邮件,问他在说什么。这是他的答案(格式编辑)。这听起来不是他打算在这里张贴答案;

I emailed Stephan and asked what he was talking about. Here's his answer (edited for formatting). It didn't sound like he was planning to post the answer here; if he does, I'll delete this copy.

我是指这个:

#include <vector>
using namespace std;

int main() {
    vector<vector<int>> v(11, 22);
}

使用VC10 SP1(C ++ 03以下)编译, VC11 RTM(在C ++ 11之后):

It compiles with VC10 SP1 (following C++03), but not with VC11 RTM (following C++11): [snip error message dump]

C ++ 03 23.1.1 [lib .sequence.reqmts] / 9 说:


对于本子句和第21节中定义的每个序列:

- 构造函数

template< class InputIterator> X(InputIterator f,InputIterator l,const Allocator& a = Allocator())
的效果应与
相同:

X(static_cast< typename X :: size_type>(f),static_cast< typename X :: value_type>(l),a)

如果 InputIterator 是一个整数类型。

For every sequence defined in this clause and in clause 21:
— the constructor
template <class InputIterator> X(InputIterator f, InputIterator l, const Allocator& a = Allocator())
shall have the same effect as:
X(static_cast<typename X::size_type>(f), static_cast<typename X::value_type>(l), a)
if InputIterator is an integral type.

这个转换的向量< vector< int& > v(11,22)向量< vector< int> v(static_cast< size_t>(11),static_cast< vector< int>(22)) static_cast 能够调用显式构造函数,例如向量的大小构造函数。)

This transformed vector<vector<int>> v(11, 22) to vector<vector<int>> v(static_cast<size_t>(11), static_cast<vector<int>>(22)), which is valid. (static_cast is capable of invoking explicit constructors, like vector's size constructor.)

C ++ 11 23.2.3 [sequence.reqmts] / 14 说:


对于本条款和第21条中定义的每个序列容器:

- 如果构造函数

template< class InputIterator> X(InputIterator first,InputIterator last,const allocator_type& alloc = allocator_type())

调用类型 InputIterator

这会删除(InIt,InIt),因为它不能作为输入迭代器,因此构造函数不会参与重载解析。 ctor来自过载分辨率。这会使(size_type n,const T& value),其中 T vector< int> ; 。然而,这将尝试将 22 隐式转换为临时向量< int> code> const T& )。 向量的大小构造函数的显式性禁止。

This removes the (InIt, InIt) ctor from overload resolution. That leaves (size_type n, const T& value), where T is vector<int>. However, that would try to implicitly convert 22 to a temporary vector<int> (in order to bind it to const T&). The explicitness of vector's size constructor forbids that.

读取另一个SO问题,这是一个不同的问题。

Reading the other SO question, this is a different issue.

STL

这篇关于什么是2D向量构造打破C ++ 11的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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