2

Is it possible to initialise a member fusion vector to a value specified in a derived class without making the base class a template class?

like this:

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
}

I know that will not work- but I hope it will make it easier to understand my goal.

  1. delaying the specification of the types the vector will contain until a class derives from it.
  2. Without specifying the types the vector should contain by making the base class a template class

If not- what is closest thing to it?

  • No, what would that even mean? – Barry May 16 '15 at 16:35
  • children in the base class should become =make_vector(string("test1"),string("test"); – Hans vesselgård May 16 '15 at 17:39
  • class container { const auto children; container (auto children):children (children){} friend ostream& operator<< (ostream& streamReceiver, const FuzzElementContainer& streamSender) { for_each(streamSender.children, [&](const auto& x) { streamReceiver << x ; }); return streamReceiver; } } 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 } void main() { derived a; cout << a; //should print "test1 test" } – Hans vesselgård May 16 '15 at 17:43

1 Answers1

1

The closest thing that doesn't require the base class to be a template is to use type erasure. You can roll your own ¹ or use Boost Type Erasure etc. Pick what suits you best.

The simplest way to achieve it would be boost::any:

Sample

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;
}

Prints

(test1 test)

¹ e.g.

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you- it dit not really solve my problem, because that requires the type of the children to be stored in the derived class. Making it impossible to iterate the children in the base class. So I guees doing what i wanted to do was indeed impossible. But I learned alot and very nice answered. Thank you – Hans vesselgård May 17 '15 at 12:24
  • If somebody finds this topic with a google search I would like them to know what I ended up doing. Instead of having the children in the base container I declared them in the derived class, and used http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern to access them from the base. – Hans vesselgård Jun 04 '15 at 19:05