C++ c++;获取Boost::Graph的顶点属性

C++ c++;获取Boost::Graph的顶点属性,c++,boost,graph,properties,C++,Boost,Graph,Properties,给定一个顶点为: class VertexProps { public: int id; float frame; string name; }; 我已经使用绑定属性初始化了boost图。我知道我可以通过以下方式获得框架: std::cout << "Vertex frame: " << boost::get(&VertexProps::frame, graph, vertex) << std::endl; //Need

给定一个顶点为:

class VertexProps {
  public:
    int id;
    float frame;
    string name;
};
我已经使用绑定属性初始化了boost图。我知道我可以通过以下方式获得框架:

std::cout << "Vertex frame: " << boost::get(&VertexProps::frame, graph, vertex) << std::endl;
//Need to make this work: float frame = boost::get(&VertexProps::frame, graph, vertex);
//graph is a boost::adjacency_list and vertex is a boost::vertex_descriptor

如果在编译时没有任何宏魔法,就无法做到这一点。C++不允许字符串文字作为非类型模板参数,并且具有非常小的反射能力。 您提出(并希望避免)的解决方案需要在运行时进行一些工作,通常应该避免

宏观解决方案将遵循以下思路:

#define MY_GET(v, pname) boost::get(&VertexProps##pname, graph, v)
PropValType v = MY_GET(v, frame);

好吧,我已经说服了自己这个事实。但是有没有什么方法可以转换成正确的类型呢?我尝试了float frame=*static_cast(boost::get(&VertexProps::pname,graph,v))我不明白<代码>boost::get(&VertexProps::pname,graph,v)已返回一个
浮点值。有什么可投的?你说得对。他很累;编译器告诉我“从'void*'到'float'的转换无效;我读错了行号。”。
typedef boost::variant< int, unsigned int, float, std::string > PropValType;
typedef boost::vertex_bundle_type<Graph>::type PropIdType;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;

PropValType get_vertex_prop(Vertex& v, PropIdType pname)
{
  boost::get(pname, graph, v);
  //If pname = frame then return value as float (or cast boost::variant to float)
  //If pname = name then return value as a string
}
PropValType get_vertex_prop(Vertex& v, std::string pname) {
 if (pname == "frame") {
   boost::get(&VertexProps::frame, graph, v)
   //return value as float
 }
 if (...)
}
#define MY_GET(v, pname) boost::get(&VertexProps##pname, graph, v)
PropValType v = MY_GET(v, frame);