(型消去とは、Boost.Anyのように、クラスに関する型情報の一部またはすべてを非表示にすることを意味します。)
型消去の手法を手に入れながら、私が知っている手法を共有したいと思っています。私の希望は、誰かが彼/彼女の最も暗い時間に考えたいくつかのクレイジーなテクニックを見つけることです。:)
私が知っている、最初で最も明白で一般的に採用されているアプローチは、仮想関数です。クラスの実装をインターフェースベースのクラス階層内に隠すだけです。多くのBoostライブラリがこれを実行します。たとえば、Boost.Anyはこれを実行してタイプを非表示にし、Boost.Shared_ptrはこれを実行して(割り当て解除)割り当てメカニズムを非表示にします。
次にvoid*
、Boost.Functionがファンクタの実際の型を非表示にするように、実際のオブジェクトをポインタに保持しながら、テンプレート化された関数への関数ポインタを持つオプションがあります。実装例は質問の最後にあります。
それで、私の実際の質問のために:
あなたは他にどんなタイプの消去テクニックを知っていますか?可能な場合は、サンプルコード、使用例、それらについての経験、および詳細を読むためのリンクを提供してください。
編集
(これを回答として追加するか、質問を編集するだけか確信が持てなかったので、安全
な方法を実行します。)仮想関数や操作なしで何かの実際のタイプを非表示にするもう1つの優れた手法void*
は、 1つのGManがここで採用しています。これがどのように機能するかについての私の質問に関連しています。
コード例:
#include <iostream>
#include <string>
// NOTE: The class name indicates the underlying type erasure technique
// this behaves like the Boost.Any type w.r.t. implementation details
class Any_Virtual{
struct holder_base{
virtual ~holder_base(){}
virtual holder_base* clone() const = 0;
};
template<class T>
struct holder : holder_base{
holder()
: held_()
{}
holder(T const& t)
: held_(t)
{}
virtual ~holder(){
}
virtual holder_base* clone() const {
return new holder<T>(*this);
}
T held_;
};
public:
Any_Virtual()
: storage_(0)
{}
Any_Virtual(Any_Virtual const& other)
: storage_(other.storage_->clone())
{}
template<class T>
Any_Virtual(T const& t)
: storage_(new holder<T>(t))
{}
~Any_Virtual(){
Clear();
}
Any_Virtual& operator=(Any_Virtual const& other){
Clear();
storage_ = other.storage_->clone();
return *this;
}
template<class T>
Any_Virtual& operator=(T const& t){
Clear();
storage_ = new holder<T>(t);
return *this;
}
void Clear(){
if(storage_)
delete storage_;
}
template<class T>
T& As(){
return static_cast<holder<T>*>(storage_)->held_;
}
private:
holder_base* storage_;
};
// the following demonstrates the use of void pointers
// and function pointers to templated operate functions
// to safely hide the type
enum Operation{
CopyTag,
DeleteTag
};
template<class T>
void Operate(void*const& in, void*& out, Operation op){
switch(op){
case CopyTag:
out = new T(*static_cast<T*>(in));
return;
case DeleteTag:
delete static_cast<T*>(out);
}
}
class Any_VoidPtr{
public:
Any_VoidPtr()
: object_(0)
, operate_(0)
{}
Any_VoidPtr(Any_VoidPtr const& other)
: object_(0)
, operate_(other.operate_)
{
if(other.object_)
operate_(other.object_, object_, CopyTag);
}
template<class T>
Any_VoidPtr(T const& t)
: object_(new T(t))
, operate_(&Operate<T>)
{}
~Any_VoidPtr(){
Clear();
}
Any_VoidPtr& operator=(Any_VoidPtr const& other){
Clear();
operate_ = other.operate_;
operate_(other.object_, object_, CopyTag);
return *this;
}
template<class T>
Any_VoidPtr& operator=(T const& t){
Clear();
object_ = new T(t);
operate_ = &Operate<T>;
return *this;
}
void Clear(){
if(object_)
operate_(0,object_,DeleteTag);
object_ = 0;
}
template<class T>
T& As(){
return *static_cast<T*>(object_);
}
private:
typedef void (*OperateFunc)(void*const&,void*&,Operation);
void* object_;
OperateFunc operate_;
};
int main(){
Any_Virtual a = 6;
std::cout << a.As<int>() << std::endl;
a = std::string("oh hi!");
std::cout << a.As<std::string>() << std::endl;
Any_Virtual av2 = a;
Any_VoidPtr a2 = 42;
std::cout << a2.As<int>() << std::endl;
Any_VoidPtr a3 = a.As<std::string>();
a2 = a3;
a2.As<std::string>() += " - again!";
std::cout << "a2: " << a2.As<std::string>() << std::endl;
std::cout << "a3: " << a3.As<std::string>() << std::endl;
a3 = a;
a3.As<Any_Virtual>().As<std::string>() += " - and yet again!!";
std::cout << "a: " << a.As<std::string>() << std::endl;
std::cout << "a3->a: " << a3.As<Any_Virtual>().As<std::string>() << std::endl;
std::cin.get();
}
shared_ptr
これを反映していませんshared_ptr<int>
。たとえば、標準のコンテナとは異なり、常に同じです。
As
(s)関数はそのように実装されません。私が言ったように、決して安全に使用できません!:)
function
、shared_ptr
、any
、など?彼らはすべて甘い甘いユーザーの利便性のために型消去を採用しています。