2

基本クラスをテンプレート クラスにせずに、メンバーの融合ベクトルを派生クラスで指定された値に初期化することは可能ですか?

このような:

class container
{
const auto children;
container (auto children):children (children){}
}

class derived : public container
{
derived():container(make_vector(string("test1"),string("test"))){} // http://www.boost.org/doc/libs/1_57_0/libs/fusion/doc/html/fusion/container/generation/functions/make_vector.html
}

それがうまくいかないことはわかっていますが、私の目標をより簡単に理解できるようになることを願っています.

  1. クラスが派生するま​​で、ベクターに含まれる型の指定を遅らせます。
  2. 基本クラスをテンプレート クラスにすることで、ベクターに含まれる型を指定せずに

そうでない場合、それに最も近いものは何ですか?

4

1 に答える 1

1

基本クラスをテンプレートにする必要がない最も近い方法は、型消去を使用することです。自分の ¹ をロールするか、Boost Type Erasure などを使用できます。最適なものを選択してください。

それを達成する最も簡単な方法は次のboost::anyとおりです。

サンプル

Live On Coliru

#include <boost/any.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <string>

namespace fus = boost::fusion;

class container
{
  protected:
    boost::any children;

    template <typename T>
    container (T const& children) : children(children) {}
};

class derived : public container
{
    using V = boost::fusion::vector2<std::string, std::string>;
  public:
    derived() : 
        container(fus::make_vector(std::string("test1"),std::string("test"))){} 

    friend std::ostream& operator<<(std::ostream& os, derived const& d) {
        return os << boost::any_cast<V const&>(d.children);
    }
};

#include <iostream>

int main() {
    derived d;
    std::cout << d;
}

版画

(test1 test)

¹例

于 2015-05-16T19:00:49.797 に答える