私が理解している限り、自分のコンテナーに C++ のアロケーターを使用する理由の 1 つは、割り当てと構築を分離できることです。
さて、これが std::tuples で次の方法で可能かどうか疑問に思います: std::tuple を構築するたびに、スペースは予約されますが、オブジェクトは (まだ) 構築されません。代わりに、必要なときに i 番目の引数を作成するためにアロケータを使用できます。
擬似コード:
struct my_struct {
const bool b; // note that we can use const
my_struct(int x) : b(x==42) {}
};
int main()
{
std::tuple<int, my_struct> t;
// the tuple knows an allocator named my_allocator here
// this allocator will force the stack to reserve space for t,
// but the contained objects are not constructed yet.
my_allocator.construct(std::get<0>(t), 42);
// this line just constructed the first object, which was an int
my_allocator.construct(std::get<1>(t), std::get<0>(t));
// this line just constructed the 2nd object
// (with help of the 1st one
return 0;
}
考えられる問題の 1 つは、通常、アロケーターが型にバインドされているため、型ごとに 1 つのアロケーターが必要になることです。もう 1 つの問題は、std::tuple のメモリをヒープに割り当てる必要があるかどうか、またはスタックが機能するかどうかです。どちらも私にとっては問題ありません。
それでも何とか出来るのでしょうか?そうでない場合は、自分で作成したアロケーターでこれを行うことができますか?