6

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はそれが好きです。
クランはそれが好きです。
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 Aligned_allocator &' に

ブール値の質問: これはバグですか? もしそうなら、私はそれを提出しようとします.

[編集: コメントに記載されているように、これはデバッグ モードでのみ発生します。リリース モードでは、問題なくコンパイルおよび実行されます。] [編集: より単純な例]

4

1 に答える 1

6

ブール値の質問: これはバグですか?

false.


ここで MSVC によって提供されたテンプレート エラーは非常に役に立ちませんが、ここでのエラーは私のものです (このバージョンの標準ライブラリは今日出荷されているので安心です)。

私はこのアロケーター (および後に縮小されたテスト ケース) をさまざまなソースから作成したため、それが正しいと思い込んでいました。ただし、コメントで示唆されているように、今回はドキュメントを徹底的に確認しました。

ここで欠落しているコンポーネントは、コピー コンストラクター (自動生成できないテンプレート) の 1 つです。rebindこれは、構造体が親クラスの同じ構造体をオーバーライドするため、構造体が定義されている場合にのみ表示されrebindます (親クラスにあるため、最終的には親のコピー コンストラクターが呼び出されるため、問題はありません (技術的に間違っていることを除いて) )))。


ここで興味深いのは、エラーが今まで発生しなかったことです。私が言ったように、GCC、Clang、および MSVC 2013 はすべて (それぞれのデバッグ モードであっても) 気に入っています。これらのどれも、たまたまテンプレート コピー コンストラクターを呼び出していなかっただけです。それにもかかわらず、それ標準で指定されているため、繰り返しますが、エラーは最終的には私のものです。

MSVC コンパイラ チームの皆さん、おめでとうございます。お騒がせして申し訳ありません。:D

于 2015-07-20T18:09:57.447 に答える