c++ - boost 业力 : how does this implicit call to transform_attribute work?(或没有?)

标签 c++ c++11 boost boost-spirit-karma

我有以下一段代码似乎工作正常(我基于 reuse parsed variable with boost karma 的语义操作)。

#include <iostream>
#include <iterator>
#include <memory>
#include <string>
#include <vector>

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/sequence.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
#include <boost/spirit/include/support_attributes.hpp>
#include <boost/spirit/include/support_adapt_adt_attributes.hpp>

using namespace boost::spirit;

struct DataElement
{
  DataElement(const std::string& s) : str_(s) {}

  const std::string& str() const { return str_; }
  std::string& str() { return str_; }
  std::string str_;
};
using Data = std::vector<std::shared_ptr<const DataElement>>;

namespace boost {
  namespace spirit {
    namespace traits {

      template<>
      struct transform_attribute<std::shared_ptr<const DataElement> const, const DataElement&, karma::domain>
      {
        using type = const DataElement&;
        static type pre(const std::shared_ptr<const DataElement>& val) { return *val; }
      };

    }
  }
}

BOOST_FUSION_ADAPT_ADT(
  DataElement,
  (std::string&, const std::string&, obj.str(), obj.str())
  );

template<typename Iterator>
struct TheGrammar: public karma::grammar<Iterator, Data()>
{
  TheGrammar(): karma::grammar<Iterator, Data()>(start)
  {
    start %= -(elt % karma::eol);
    elt %=
      karma::lit("'some prefix'")
      << karma::string [karma::_1 = boost::phoenix::at_c<0>(karma::_val)]
      << karma::lit("'some infix 1'")
      << karma::string [karma::_1 = boost::phoenix::at_c<0>(karma::_val)]
      << karma::lit("'some infix 2'")
      << karma::string [karma::_1 = boost::phoenix::at_c<0>(karma::_val)]
      << karma::lit("'some suffix'")
      ;
  }

  karma::rule<Iterator, Data()> start;
  karma::rule<Iterator, const DataElement&()> elt;
};

int main(void)
{
  Data vec = {
    std::make_shared<DataElement>("one"),
    std::make_shared<DataElement>("two"),
    std::make_shared<DataElement>("three"),
    std::make_shared<DataElement>("four"),
    std::make_shared<DataElement>("five"),
    std::make_shared<DataElement>("six"),
    std::make_shared<DataElement>("seven"),
    std::make_shared<DataElement>("eight"),
  };
  using iterator_type = std::ostream_iterator<char>;
  iterator_type out(std::cout);

  TheGrammar<iterator_type> grammar;
  return karma::generate(out, grammar, vec);
}

我想了解一些事情:

  1. 为什么我不需要使用 karma::attr_cast任何地方?我的start规则是 std::shared_ptr 的 vector 而 elt规则适用于实际对象 const 引用。我最初试过 attr_cast但无处可去,只是半心半意地尝试了这个版本,以防万一它起作用,而且它起作用了……
  2. 如果我注释掉我的自定义 transform_attribute 为什么它仍然可以编译共?有没有默认的std::shared_ptr<T> -> T& transform_attribute 提供了吗?我找不到太多东西,但也许我没有找对地方?
  3. 如果我注释掉我的习惯 transform_attribute ,如上所述,代码仍然编译,但在运行时显然存在一些内存损坏。 karma::string产生垃圾。在某种程度上,我可以理解一定会发生一些有趣的事情,因为我什至没有告诉 karma 如何从我的 shared_ptr 中获取。到对象。它编译了实际的错误/错误吗?

非常感谢您的宝贵时间和帮助!

最佳答案

  1. 每个规则都有一个隐含的 attr_cast 声明的属性类型
  2. 有些时候 Spirit 的类型兼容性规则变得一团糟。我所看到的只是它与字符串类型是一个容器这一事实有关。它在某个地方“复制构造”了一个长度似乎为 97332352 的 std::string。不出所料,这本身就是错误的并且恰好触发了 UB,因为最终传递给 memset 的范围重叠:

    Source and destination overlap in memcpy(0x60c1040, 0x5cd2c90, 97332352)
       at 0x4C30573: memcpy@@GLIBC_2.14 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
       by 0x401B26: copy (char_traits.h:290)
       by 0x401B26: _S_copy (basic_string.h:299)
       by 0x401B26: _S_copy_chars (basic_string.h:342)
       by 0x401B26: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) [clone .isra.53] (basic_string.tcc:229)
       by 0x402442: _M_construct_aux<char*> (basic_string.h:195)
       by 0x402442: _M_construct<char*> (basic_string.h:214)
       by 0x402442: basic_string (basic_string.h:401)
       by 0x402442: call<const boost::spirit::unused_type> (extract_from.hpp:172)
       by 0x402442: call<const boost::spirit::unused_type> (extract_from.hpp:184)
       by 0x402442: extract_from<std::__cxx11::basic_string<char>, boost::fusion::extension::adt_attribute_proxy<DataElement, 0, true>, const boost::spirit::unused_type> (extract_from.hpp:217)
       by 0x402442: extract_from<std::__cxx11::basic_string<char>, boost::fusion::extension::adt_attribute_proxy<DataElement, 0, true>, const boost::spirit::unused_type> (extract_from.hpp:237)
       by 0x402442: pre (attributes.hpp:23)
    
  3. 是的,这是一个 QoI 问题。

    问题通常出在 C++ 的隐式转换上。指针类型有许多意想不到的转换。共享指针确实将它们的上下文转换为 bool。

更多注意事项:

  1. 您的 fusion 改编似乎有缺陷:val 未在 setter 中使用

    BOOST_FUSION_ADAPT_ADT(DataElement, (std::string &, const std::string &, obj.str(), obj.str() = val))
    
  2. 你正在做很多我学会避免的事情。

关于c++ - boost 业力 : how does this implicit call to transform_attribute work?(或没有?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37925961/

相关文章:

c++ - 有什么方法可以避免跨类的不同构造函数进行代码复制?

c++ - 如何获取 boost::fusion::map 中值的类型?

c++ - 为什么我的峰度是负数?

c++ - 使用 boost::geometry::buffer 函数

c++ - 制作动态循环队列,元素输出正常但程序崩溃?

c++ - 如果我分配给彼此的两个变量都是字符串类型,为什么我会收到这个 strcpy 分配错误?

c++ - 对 `main' 的 undefined reference ,当 main 存在时

c++ - 使用 bind2nd() : "member function already defined or declared" instead of "reference to reference" 的奇怪编译器错误

C++ u8 literal char 覆盖

c++ - 基于 CRTP 的解决方案会是什么样子?