enum {N = 10, M = 100};
vector<int> v(N, M);
失敗與Visual Studio 2013來編譯,由於以下錯誤:
error C2838: 'iterator_category' : illegal qualified name in member declaration
這有什麼錯呢?
enum {N = 10, M = 100};
vector<int> v(N, M);
失敗與Visual Studio 2013來編譯,由於以下錯誤:
error C2838: 'iterator_category' : illegal qualified name in member declaration
這有什麼錯呢?
這是VS2012和VS2013中的一個錯誤,因爲它不符合C++ 11標準(_HAS_CPP0X
定義爲1):
C++ 03 23.1.1 [lib.sequence.reqmts]/9表示:
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)
ifInputIterator
is an integral type.
但是從C++ 11 23.2.3 [sequence.reqmts]/14:
For every sequence container defined in this Clause and in Clause 21:
— If the constructor
template <class InputIterator> X(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type())
is called with a typeInputIterator
that does not qualify as an input iterator, then the constructor shall not participate in overload resolution.
該構造函數不應該被認爲在所有
這裏更多:https://stackoverflow.com/a/12432482/1938163
作爲解決方法,您可以「幫助超載分辨率位」,例如,
std::vector<int> v(static_cast<std::vector<int>::size_type>(N), M);
由於C++ 11 vector
構造函數接受兩個InputIterators
應該被禁用,如果兩個參數不是迭代器。 VS2013未能正確實施。
這是一個Visual Studio 2013的錯誤,從錯誤的產生(see it live),這只是一小部分:
[...]
see reference to function template instantiation 'std::vector>::vector<,void>(_Iter,_Iter)' being compiled
[...]>
它試圖使用帶有兩個輸入迭代構造。這將是一個錯誤,這兩個gcc
和clang
罰款與此代碼。
我們可以在C++ 11 that constructor不應被視爲:
Constructs the container with the contents of the range [first, last). This constructor does not participate in overload resolution if InputIt does not satisfy InputIterator, to avoid ambiguity with the overload 2 (since C++11)
這符合草案C++ 11個標準節23.2.3
序列容器款它說:
If the constructor
template <class InputIterator> X(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type())
is called with a type InputIterator that does not qualify as an input iterator, then the constructor shall not participate in overload resolution.
gcc和clang都可以,這看起來像一個bug。 – 2014-09-18 17:06:34
我強烈懷疑VS的錯誤.. – 2014-09-18 17:06:46