いいえ。boost::anyでもboost::Variantでもありません(「operator +をサポートできる可能性のあるすべてのタイプを列挙せずに」要件を修飾しません)。
あなたがする必要があるのはあなた自身のものを作ることです。boost::anyの背後にある概念は非常に単純です。ドキュメントを見ると、テクニックを説明する記事へのリンクがあります(基本的には、ポリモーフィズムを備えたハンドル/ボディのイディオムです)。あなたがする必要があるのは、あなたの様々なオブジェクトが持っていなければならないインターフェースを決定し、「any」インターフェースを書くことです、そしてそれはそれに応じて実装されます。そのようなものに似ているもの:
struct my_any
{
template < typename T >
my_any(T const& t) : pimpl(new impl<T>(t)) {}
...
some_type get_some_type() const;
...
private:
struct impl_base
{
....
virtual some_type get_some_type() const = 0;
};
template < typename T >
struct impl : impl_base
{
some_type get_some_type() const { return t.get_some_type(); }
impl(T const& t_var) : t(t_var) {}
....
};
boost::scoped_ptr<impl_base> pimpl;
};
some_type operator+ (my_any const& a, my_any const& b)
{
return a.get_some_type() + b.get_some_type();
}
ジェネリック型でoperator+が何をするか想像するのは難しいので、私は少し意味のあるものを作りました。もちろん、ニーズに合わせて変更する必要があります。