【问题标题】:Boost property_tree: multiple values per key, on a template classBoost property_tree:模板类上每个键的多个值
【发布时间】:2015-04-22 14:39:16
【问题描述】:

Boost property_tree: multiple values per key这个话题上,提出了一种读取boost树的多个键的方法。我将在下面粘贴修改后的版本

template<int dim>
struct my_vector
{
  std::vector<double> a_vector;
};

翻译者是:

template<int dim>
struct my_vector_translator
{
  typedef std::string    internal_type;
  typedef my_vector external_type;
  // Get a my_vector from a string
  boost::optional<external_type> get_value(const internal_type& str)
  {
    if (!str.empty())
    {
      std::vector val;
      val.resize(dim)
      std::stringstream s(str);
      double temp;
      for (int i=0, i < dim; ++i){
          s >> temp;
          val.a_vector[i] = temp;
      return boost::optional<external_type>(val);
    }
    else
      return boost::optional<external_type>(boost::none);
  }

};

现在,我应该如何正确注册翻译?

namespace boost { namespace property_tree 
{
  template<typename Ch, typename Traits, typename Alloc> 
  struct translator_between<std::basic_string< Ch, Traits, Alloc >, my_vector<it want something here of course, where do i get it from ?> >
  {
    typedef my_vector_translator<it want something here of course, where do i get it from ?> type;
  };
} }

在你包含前面的代码之后,你可以使用property_tree作为:

  pt.get<my_vector ??and here where do i put the size ?>("box.x");

不知何故,我觉得 my_vector 类没用。我不能直接使用 std::vector 吗?

我想要类似的东西:

pt.get<std::vector<double>, 3>("mesh.a_line_where_i_know_i_have_3_doubles_to_read");

谢谢。

【问题讨论】:

  • 没有SSCCE,这不会去任何地方
  • 嗯,SSCCE 包含在 J. Calleja 在另一个主题中给出的答案中。我的问题很简单,如果 low_high_value 是一个接受模板的类,如何扩展它。如果您认为有必要,我可以复制粘贴他们的示例。
  • 是的。一定要告诉我们您遇到的问题。
  • 好的,我插入了一个更详细的示例。感谢您的宝贵时间。
  • 您可以使用 vecetor 进行存储,但您仍然需要元数据(大小)可用。所以变化不大

标签: c++ templates boost boost-propertytree


【解决方案1】:

在我看来 size 已经是 my_vector 类模板的模板参数了。

所以

pt.get<my_vector<3> >("box.x");

应该不错

注册:

部分特化可以引入额外的模板参数,所以只需将size_t N 添加到列表中:

namespace boost { namespace property_tree {
    template<typename Ch, typename Traits, typename Alloc, size_t N> 
        struct translator_between<std::basic_string<Ch, Traits, Alloc>, my_vector<N> >
        {
            typedef my_vector_translator<N> type;
        };
} }

【讨论】:

    猜你喜欢
    • 2012-10-15
    • 2017-10-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多