提高::变种 - 获取会员的矢量特性 [英] boost::variant - get vector property of members
问题描述
我坚持:)。我有两个层次结构,每个级别有子节点。
该目标是利用这种结构来填充GUI树。我试图访问的变体成员的子节点的通用方式。继code不能编译,使用vs2013。我将AP preciate了援助之手和/或建议
对设计变更。
的#includestdafx.h中
#包括LT&;内存和GT;
#包括LT&;串GT;
#包括LT&;矢量>
#包括LT&;升压/ variant.hpp>阶级基础{};A类:公共基础
{
上市:
的std ::矢量<的std :: shared_ptr的<碱基GT;> &安培; LST(){返回_lst; }
私人的:
的std ::矢量<的std :: shared_ptr的<碱基GT;> _lst;
};B类:公共基础
{
上市:
的std ::矢量<的std :: shared_ptr的< A>>&安培; LST(){返回_lst; }
私人的:
的std ::矢量<的std :: shared_ptr的< A>> _lst;
};使用bstvar =的boost ::变体LT; A,B取代;类lstVisitor:公众的boost :: static_visitor<>
{
上市:
模板< typename的T>的std ::矢量<的std :: shared_ptr的<碱基GT;>&安培;运算符()(T& T公司)常量
{
返回t.lst();
}
};INT _tmain(INT ARGC,_TCHAR *的argv [])
{
bstvar试验;
汽车及放大器; LST =的boost :: apply_visitor的(lstVisitor {},试); 返回0;
}
您游客有一个隐含的返回类型无效
(模板参数缺失)。
下面是借机除去基类,因为它是在大多数C ++ 11 code碱基不再需要:
<大骨节病> 住在Coliru 骨节病>
的#include&LT;内存和GT;
#包括LT&;串GT;
#包括LT&;矢量&GT;
#包括LT&;升压/ variant.hpp&GT;阶级基础{};使用base_vec =的std ::矢量&lt;的std :: shared_ptr的&LT;碱基GT;取代;A类:公共基础{
上市:
base_vec&安培; LST(){返回_lst; } 私人的:
base_vec _lst;
};B类:公共基础{
上市:
base_vec&安培; LST(){返回_lst; } 私人的:
base_vec _lst;
};使用bstvar =的boost ::变体LT; A,B取代;结构lstVisitor {
使用result_type的= base_vec&放;;
模板&LT; typename的T&GT; result_type的运算符()(T&amp; T公司)const的{返回t.lst(); }
};INT主(INT ARGC,CHAR *的argv []){
bstvar测试{B {}};
base_vec&安培; LST =的boost :: apply_visitor的(lstVisitor {},试); 返回0;
}
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:
#include <memory>
#include <string>
#include <vector>
#include <boost/variant.hpp>
class base {};
using base_vec = std::vector<std::shared_ptr<base> >;
class A : public base {
public:
base_vec &lst() { return _lst; }
private:
base_vec _lst;
};
class B : public base {
public:
base_vec &lst() { return _lst; }
private:
base_vec _lst;
};
using bstvar = boost::variant<A, B>;
struct lstVisitor {
using result_type = base_vec&;
template <typename T> result_type operator()(T &t) const { return t.lst(); }
};
int main(int argc, char *argv[]) {
bstvar test { B{} };
base_vec& lst = boost::apply_visitor(lstVisitor{}, test);
return 0;
}
这篇关于提高::变种 - 获取会员的矢量特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!