2

私はBoost.Interprocessのドキュメントを何時間も見つめてきましたが、それでもこれを理解することができませんでした。ドキュメントには、次のように共有メモリにベクターを作成する例があります。

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef vector<int, ShmemAllocator> MyVector;

int main(int argc, char *argv[])
{
    //Create a new segment with given name and size
    managed_shared_memory segment(create_only, "MySharedMemory", 65536);
    //Initialize shared memory STL-compatible allocator
    const ShmemAllocator alloc_inst (segment.get_segment_manager());
    //Construct a vector named "MyVector" in shared memory with argument alloc_inst
    MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);

今、私はこれを理解しています。私が立ち往生しているのはsegment.construct()、要素の数を指定するために2番目のパラメーターを渡す方法です。construct()プロセス間ドキュメントは、としてのプロトタイプを提供します

MyType *ptr = managed_memory_segment.construct<MyType>("Name") (par1, par2...);

でもやってみると

MyVector *myvector = segment.construct<MyVector>("MyVector")(100, alloc_inst);

コンパイルエラーが発生します。

私の質問は次のとおりです。

  1. オブジェクトのコンストラクターである、par1, par2からパラメーターを実際に渡されるのは誰ですか。私の理解では、テンプレートアロケータパラメータが渡されています。あれは正しいですか?segment.constructvector
  2. alloc_inst共有メモリに作成されているオブジェクトのコンストラクターに必要なパラメーターに加えて、別のパラメーターを追加するにはどうすればよいですか?

これに関する簡潔なBoostドキュメント以外の情報はほとんどありません。

4

1 に答える 1

3

Boostユーザーのメーリングリストで同じ質問をしたところ、Steven Watanabeは、問題は単純であると答えました。std:: vectorには、タイプ(size、allocator)のコンストラクターがありません。そのドキュメントを見ると、コンストラクターは

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

したがって、正しい呼び出しは

MyVector *myvector = segment.construct<MyVector>("MyVector")(100, 0, alloc_inst);

エレメンタリー、私の愛するワトソン、エレメンタリー!

于 2010-03-13T15:35:02.600 に答える