且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

隐式构造函数转换工作在显式vector :: vector上,只有有时

更新时间:2022-12-16 08:18:06

您对Comeau的假设隐含地调用 explicit 构造函数很可能不正确。

Your assumption about Comeau implicitly calling an explicit constructor is most likely incorrect. The behavior is indeed broken, but the problem is different.

我怀疑这是在Comeau自带的标准库的实现中的错误,而不是核心Comeau编译器

I suspect that this is a bug in the implementation of Standard Library that comes with Comeau, not with core Comeau compiler itself (although the line is blurry in this case).

如果你构建一个具有类似于 std :: vector的构造函数属性的快速虚拟类并尝试相同的事情,你会发现编译器正确地拒绝执行构造。

If you build a quick dummy class that has constructor properties similar to std::vector and try the same thing, you'll discover that the compiler correctly refuses to perform construction.

它接受你的代码的最可能原因是 std :: vector 的双参数构造函数的众所周知的正式模糊性。它可以解释为(大小,初始值)构造函数

The most likely reason why it accepts your code is the well-known formal ambiguity of two-parameter constructor of std::vector. It can be interpreted as (size, initial value) constructor

explicit vector(size_type n, const T& value = T(),
                const Allocator& = Allocator());

(begin,end)构造函数接受两个迭代器

or as (begin, end) template constructor with the latter accepting two iterators

template <class InputIterator>
  vector(InputIterator first, InputIterator last,
         const Allocator& = Allocator());

标准库规范明确指出,当两个整数值用作参数时,实现必须确保不知道如何选择形成的构造函数,即(size,initial value)。怎么做 - 没关系。它可以在库级别完成,它可以在核心编译器中硬编码。

The standard library specification explicitly states that when two integral values are used as arguments, the implementation must make sure somehow that the formed constructor is selected, i.e. (size, initial value). How it is done - doesn't matter. It can be done at the library level, it can be hardcoded in the core compiler. It can be done in any other way.

但是,为了响应(20,40)参数Comeau编译器似乎用 InputIterator = int 错误地选择和实例化后一个构造函数。我不知道它是如何管理编译专用版本的构造函数,因为整数值不能和不会作为迭代器。

However, in response to ( 20, 40 ) arguments Comeau compiler appears to erroneously select and instantiate the latter constructor with InputIterator = int. I don't know how it manages to compile the specialized version of the constructor, since integral values can't and won't work as iterators.

如果你尝试这个

vector< vector< int > > v( 20U, 40 );

你会发现编译器现在报告错误(因为它不能再使用two-迭代器版本的构造函数),并且第一个构造函数上的 explicit 阻止它将 40 转换为 std :: vector

you'll discover that the compiler reports an error now (since it can no longer use the two-iterator version of the constructor) and the explicit on the first constructor prevents it from converting 40 to a std::vector.

同样的事情发生在 assign 。这当然是Comeau实现的缺陷,但是,实验表明,很可能需要的行为应该在库级别执行(核心编译器似乎工作正常),并且以某种方式做错了。

The same thing happens with assign. This certainly a defect of Comeau implementation, but, once again, experiments show that most likely the required behavior was supposed to be enforced at the library level (the core compiler seems to work OK), and somehow it got done incorrectly.


第二个想法,我看到我的解释中的主要想法是正确的,是错误的。另外,我可能错了在Comeau调用它是一个问题。 Comeau可能就在这里。

On the second thought, I see that the main idea in my explanation is correct, but the details are wrong. Also, I can be wrong about calling it a problem in Comeau. It is possible that Comeau is right here.

标准在23.1.1 / 9中说,

The standard says in 23.1.1/9 that


/ p>

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)




如果InputIterator是一个整数类型

if InputIterator is an integral type

上面是从字面上解释,编译器允许假设一个明确的 static_cast 在那里暗示(好...所以说),代码是合法的reason static_cast< std :: vector< int> >(10)是合法的,尽管相应的构造函数是 explicit static_cast 的存在使得编译器可以使用显式构造函数。

I suspect that if the above is interpreted literally, the compiler is allowed to assume that an explicit static_cast is implied there (well... so to say), and the code is legal for the same reason static_cast< std::vector<int> >(10) is legal, despite the corresponding constructor's being explicit. The presence of static_cast is what makes it possible for the compiler to use the explicit constructor.

如果Comeau编译器的行为是正确的(我怀疑它是根据标准的要求是正确的),我不知道这是否是委员会的意图留下这样的漏洞打开,并允许实现工作,可以出现在向量元素的构造函数的显式限制。

If the behavior of Comeau compiler is correct (and I suspect that it is in fact correct, as required by the standard), I wonder whether this was the intent of the committee to leave such a loophole open, and allow implementations to work arount the explicit restriction possibly present on the constructor of vector element.