c++ - 在模板类上 boost property_tree : multiple values per key,

标签 c++ templates boost boost-propertytree

关于这个话题,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");

谢谢。

最佳答案

在我看来,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;
        };
} }

关于c++ - 在模板类上 boost property_tree : multiple values per key,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801003/

相关文章:

c++ - 函数模板优先级

c++ - 如何在检测习语中要求精确的函数签名?

javascript - 如何使用带有文本框的模板,其中加载了从 ember.js 中的调用模板传递的数据

c++ - Boost Meta 状态机在延迟事件时以堆栈溢出结束

python - 在 Boost Python 中使用带有 std::wstring 的 C++ 函数的 Unicode

c++ - 在类构造函数中,定义一个 T 类型的 vector

c++ - "explicit template argument list not allowed"使用 g++,但使用 clang++ 编译?

c++ - OpenCV 执行速度(for 循环和 meanStdDev)

c++ - 这个 boost::lambda 使用有什么问题?

c++ - 在条件中使用提取运算符的返回值?