I am stuck :). I have two-level hierarchy, each level has child nodes. The goal is to use this structure to populate gui tree. I am trying to access child nodes of variant members in a generic manner. Following code does not compile, using vs2013. I'll appreciate the helping hand and/or advise on the design changes.
#include "stdafx.h"
#include <memory>
#include <string>
#include <vector>
#include <boost/variant.hpp>
class base {};
class A : public base
{
public:
std::vector<std::shared_ptr<base>> & lst(){ return _lst; }
private:
std::vector<std::shared_ptr<base>> _lst;
};
class B : public base
{
public:
std::vector<std::shared_ptr<A>>& lst() { return _lst; }
private:
std::vector<std::shared_ptr<A>> _lst;
};
using bstvar = boost::variant<A, B>;
class lstVisitor : public boost::static_visitor<>
{
public:
template <typename T> std::vector<std::shared_ptr<base>>& operator() (T& t) const
{
return t.lst();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
bstvar test;
auto& lst= boost::apply_visitor(lstVisitor{}, test);
return 0;
}
Your visitor has an implicit return type of
void
(the template argument is missing).Here's taking the opportunity to remove the base class as it's no longer needed in most c++11 code bases:
Live On Coliru