Вот SSCCE:
#include <memory>
#include <vector>
template <class T> struct my_allocator : std::allocator<T> {
//This overriding struct causes the error
template <class U> struct rebind {
typedef my_allocator<T> other;
};
//Ignore all this.
typedef std::allocator<T> base;
typename base::pointer allocate(typename base::size_type n, std::allocator<void>::const_pointer /*hint*/=nullptr) { return (T*)malloc(sizeof(T)*n); }
void deallocate(typename base::pointer p, typename base::size_type /*n*/) { free(p); }
};
int main(int /*argc*/, char* /*argv*/[]) {
std::vector<int,my_allocator<int>> vec;
return 0;
}
GCC нравится.
ICC нравится.
Clang нравится.
Даже MSVC 2013 нравится.
Но MSVC 2015 RC выплевывает:
1>------ Build started: Project: Test Alloc, Configuration: Debug Win32 ------
1> main.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(579): error C2664: 'void std::_Wrap_alloc<my_allocator<int>>::deallocate(int *,unsigned int)': cannot convert argument 1 from 'std::_Container_proxy *' to 'int *'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(579): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(574): note: while compiling class template member function 'void std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>::_Free_proxy(void)'
1> with
1> [
1> _Ty=int,
1> _Alloc=my_allocator<int>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(541): note: see reference to function template instantiation 'void std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>::_Free_proxy(void)' being compiled
1> with
1> [
1> _Ty=int,
1> _Alloc=my_allocator<int>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(668): note: see reference to class template instantiation 'std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>' being compiled
1> with
1> [
1> _Ty=int,
1> _Alloc=my_allocator<int>
1> ]
1> c:\users\ian mallett\desktop\test-alloc\main.cpp(18): note: see reference to class template instantiation 'std::vector<int,my_allocator<int>>' being compiled
Родственная программа выдает аналогичные фишинговые ошибки звучания. Вот два:
ошибка C2664: 'void std :: _ Wrap_alloc> :: deallocate (int *, unsigned int)': невозможно преобразовать аргумент 1 из 'std :: _ Container_proxy *' в 'int *'
невозможно преобразовать аргумент 1 из 'std :: _ Wrap_alloc>' в 'const align_allocator &'
Логический вопрос: это ошибка? Если это так, я буду (попробуйте), чтобы отправить его.
[РЕДАКТИРОВАТЬ: как указано в комментариях, это происходит только в режиме отладки. В режиме выпуска он компилируется и работает нормально.] [РЕДАКТИРОВАТЬ: гораздо более простой пример]
std
- это UB. - person cpplearner   schedule 19.07.2015std
. - person Captain Obvlious   schedule 19.07.2015std::
; ошибка все еще возникает. @UlrichEckhardt, запущенный в режиме выпуска, приводит к его успешной компиляции / запуску. - person imallett   schedule 19.07.2015rebind()
в вашем распределителе, которая объяснила бы такое поведение и действительно сделала бы ошибку в вашем коде. Пробовали ли вы режим диагностики для других стандартных библиотек (#define _GLIBCXX_DEBUG
)? - person Ulrich Eckhardt   schedule 19.07.2015