うーん... タイトルはちょっと言い過ぎですが、これのどの部分が問題を引き起こしているのか本当にわかりません。何度も試してみましたが、その理由を特定することはできません...
アイデアは、単一の Choice インスタンスが、そのテンプレートリストに渡された任意の型の任意の 1 つの値を格納できるようにすることです...格納されている型を追跡し、値を考慮することを除いて、ユニオンのようなものです。これにより、共用体メンバーのコンストラクターに対する C++ の制約を回避できます。
場合によっては機能しますが、クリーンアップ コードに問題があるようです。std::basic_string または引数リストに渡された同様のタイプでこの構造を使用し始めた2番目にセグメンテーション違反が発生し始めましたが、それが問題を引き起こす理由がわかりません。
これは一種の私自身の実験ですが、動作しない理由がわかりません (g++ の C++0x モードでコンパイル):
// virtual methods should provide a way of "remembering"
// the type stored within the choice at any given time
struct ChoiceValue
{
virtual void del(void* value) = 0;
virtual bool is(int choice) = 0;
};
// Choices are initialized with an instance
// of this structure in their choice buffer
// which should handle the uninitialized case
struct DefaultChoiceValue : public IChoiceValue
{
virtual void del(void* value) {}
virtual bool is(int choice) { return false; }
};
// When a choice is actually initialized with a value
// an instance of this structure (with the appropriate value
// for T and TChoice) is created and stored in the choice
// buffer, allowing it to be cleaned up later (using del())
template<int TChoice, typename T>
struct ChoiceValue
{
virtual void del(void* value) { ((T*)value)->~T(); }
virtual bool is(int choice) { return choice == TChoice; }
};
template<typename ... TAll>
struct Choice
{
};
template<typename T1, typename ... TRest>
struct Choice<T1, TRest...>
{
// these two constants should compute the buffer size needed to store
// the largest possible value for the choice and the actual value
static const int CSize = sizeof(ChoiceValue<0, T1>) > Choice<TRest...>::CSize
? sizeof(ChoiceValue<0, T1>) : Choice<TRest...>::CSize;
static const int VSize = sizeof(T1) > Choice<TRest...>::VSize
? sizeof(T1) : Choice<TRest...>::VSize;
IChoiceValue* _choice;
char* _choiceBuffer;
char* _valueBuffer;
Choice()
{
_choiceBuffer = new char[CSize];
_valueBuffer = new char[VSize];
_choice = new (_choiceBuffer) DefaultChoiceValue();
}
~Choice()
{
_choice->del(_valueBuffer);
delete[] _choiceBuffer;
delete[] _valueBuffer;
}
template<int TChoice, typename T>
T& get()
{
if(_choice->is(TChoice))
return *(T*)_valueBuffer;
else
{
_choice->del(_valueBuffer);
new (_valueBuffer) T();
_choice = new (_choiceBuffer) ChoiceValue<TChoice, T>();
return *(T*)_valueBuffer;
}
}
};
template<typename T1>
struct Choice<T1>
{
// required for the base case of a template
// with one type argument
static const int CSize = sizeof(ChoiceValue<0, T1>) > sizeof(DefaultChoiceValue)
? sizeof(ChoiceValue<0, T1>) : sizeof(DefaultChoiceValue);
static const int VSize = sizeof(T1);
// I have an implementation here as well in my code
// but it is pretty much just a copy of the above code
// used in the multiple types case
};
誰かが私が間違っていることを見つけることができれば、どうもありがとう:)