Boost.TypeErasure でメンバ変数っぽいのを定義する
基本的に Boost.TypeErasure ではメンバ関数を介してしか、元の型へアクセス出来ないんですが、operator T とか使ってメンバ変数でアクセスしようとするとこんな感じに。
[ソース]
#include <boost/type_erasure/any.hpp> #include <boost/type_erasure/any_cast.hpp> #include <boost/type_erasure/builtin.hpp> #include <boost/type_erasure/operators.hpp> #include <iostream> #include <vector> #include <list> template<typename R, typename T = boost::type_erasure::_self> struct X{ static R& apply(T& t){ return t.x; } }; namespace boost{ namespace type_erasure{ template<typename R, typename T, typename Base> struct concept_interface< ::X<R, T>, Base, T> : Base{ concept_interface(){ x.this_ = this; } struct x_value{ R const& operator =(R const& u){ return call(::X<R, T>(), *this_) = u; } operator R&(){ return call(::X<R, T>(), *this_); } operator R const&() const{ return call(::X<R, T>(), *this_); } concept_interface* this_; } x; }; } // namespace type_erasure } // namespace boost struct vec{ float x, y, z; }; int main(){ namespace t = boost::type_erasure; namespace m = boost::mpl; vec v; v.x = 0.3f; v.y = 1.2f; v.z = 2.4f; t::any<X<float>, t::_self&> any_vec(v); std::cout << any_vec.x << std::endl; any_vec.x = -3.14f; std::cout << any_vec.x << std::endl; std::cout << v.x << std::endl; return 0; }