MSVC 2015为std :: vector选择不正确的构造函数重载 [英] MSVC 2015 choosing incorrect constructor overload for std::vector
问题描述
以下是具有动态行数和列数的矩阵类的一部分,此类在行主要顺序中使用 std :: vector
来存储矩阵 / p>
模板< typename _Ty,
class _Alloc = std :: allocator< _Ty>
> class dynamic_matrix {
public:
typedef _Ty value_type;
typedef std :: size_t size_type;
typedef _Alloc allocator_type;
//各种其他typedef,在这里不相关...
显式dynamic_matrix(size_type _rows,size_type _cols,const _Alloc& alloc = _Alloc())
:mtx(_rows * _cols,alloc ),rows _(_ rows),cols _(_ cols){}
explicit dynamic_matrix(size_type _rows,size_type _cols,const value_type& _val,
const _Alloc& alloc = _Alloc()):mtx(_rows * _cols ,_val,alloc),rows _(_ rows),cols _(_ cols){}
//其他构造函数和方法省略...
private:
std :: vector< _Ty,_Alloc> mtx;
size_type rows_;
size_type cols_;
};
当我尝试构造一个 dynamic_matrix
使用上面显示的代码段中的第一个构造函数进行以下测试:
int main (void){
dynamic_matrix< int> dm(10,10);
}
我从 MSVC 2015
:
std :: vector< _Ty,_Alloc> :: vector(std :: initializer_list< int& ,const std :: allocator< _Ty>&)
/ pre>
:不能将参数2从'const std :: allocator< _Ty>'转换为'const int&'
而使用以下命令在
GCC 6.1.0
中编译此命令不会产生警告或错误, p>
g ++ - 6.1.0 --std = c ++ 14 -Wall -pedantic -o maintest main.cpp dynamic_matrix.h
使用
dynamic_matrix
代码段中的第二个构造函数
问题似乎是MSVC由于某种原因解释构造函数调用
mtx(_rows * _cols,alloc)
作为此参考,这将解释无法从const std :: allocator转换为const int&
错误消息。显示译文显示原文双语对照语言切换进行中,请稍候...分享|为什么MSVC不选择正确的构造函数从std :: vector
而GCC是什么,我该怎么办才能减轻这个问题?解决方案MSVS中的向量头没有没有具有形式
的构造函数
显式向量(size_type count,const Allocator& alloc = Allocator
但它有
显式向量(size_type _Count)
构造函数在C ++ 14中更改为前构造函数。看来MSVS还没有赶上这个变化。
真正奇怪的是
向量
在头文件中有正确的构造函数,如果你使用dynamic_matrix< bool> dm(10,10);
它会编译。
与MS提交了错误报告,您可以看到它此处
作为一个解决方法,直到这是固定的,你可以使用
$的形式的构造函数b $ bvector(size_type count,
const T& value,
const Allocator& alloc = Allocator());
并提供一个值来构建元素。
The following is part of my matrix class with a dynamic number of rows and columns, this class uses a
std::vector
in row-major order to store the matrix elements.
dynamic_matrix
template<typename _Ty, class _Alloc = std::allocator<_Ty> > class dynamic_matrix { public: typedef _Ty value_type; typedef std::size_t size_type; typedef _Alloc allocator_type; // various other typedefs, not relevant here... explicit dynamic_matrix(size_type _rows, size_type _cols, const _Alloc& alloc = _Alloc()) : mtx(_rows*_cols, alloc), rows_(_rows), cols_(_cols) {} explicit dynamic_matrix(size_type _rows, size_type _cols, const value_type& _val, const _Alloc& alloc = _Alloc()) : mtx(_rows*_cols, _val, alloc), rows_(_rows), cols_(_cols) {} // other constructors and methods omitted... private: std::vector<_Ty, _Alloc> mtx; size_type rows_; size_type cols_; };
When I try to construct a
dynamic_matrix
using the first constructor in the snippet shown above with the following test,int main(void) { dynamic_matrix<int> dm(10,10); }
I get the following error from
MSVC 2015
:std::vector<_Ty,_Alloc>::vector(std::initializer_list<int>,const std::allocator<_Ty>&) : cannot convert argument 2 from 'const std::allocator<_Ty>' to 'const int&'
Whereas compiling this in
GCC 6.1.0
with the following command yields no warnings nor errors,g++-6.1.0 --std=c++14 -Wall -pedantic -o maintest main.cpp dynamic_matrix.h
Using the second constructor in the
dynamic_matrix
code snippet above compiles fine for both GCC and MSVC.The issue appears to be that MSVC is, for some reason, interpreting the constructor call
mtx(_rows*_cols, alloc)
as the 7th constructor from this reference which would explain thecannot convert from const std::allocator to const int&
error message. Whereas it appears GCC is using the 3rd constructor from the above reference as I intended.Why is MSVC not choosing the correct constructor to call from
std::vector
whilst GCC is and what can I do to mitigate this?解决方案Looking through the vector header in MSVS there is no constructor with the form
explicit vector( size_type count, const Allocator& alloc = Allocator() )
But it does have
explicit vector(size_type _Count)
Which is a C++11 added constructor that was changed in C++14 to be former constructor. It appears that MSVS has not caught up with that change yet.
What is really odd is that the specialization for
vector<bool>
does have the proper constructor in the header file and if you usedynamic_matrix<bool> dm(10, 10);
it will compile.
I have filed a bug report with MS and you can see it here
As a work around until this is fixed you can use the constructor with the form of
vector( size_type count, const T& value, const Allocator& alloc = Allocator());
and supply a value to construct the elements with.
这篇关于MSVC 2015为std :: vector选择不正确的构造函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!