C++ 使用phoenix构造时调用析构函数
我正在做一个Boost Spirit Qi项目,它使用C++ 使用phoenix构造时调用析构函数,c++,boost,boost-spirit-qi,boost-phoenix,C++,Boost,Boost Spirit Qi,Boost Phoenix,我正在做一个Boost Spirit Qi项目,它使用phoenix::construct创建一个指向另一个对象的指针的对象。我注意到使用phoenix::construct在某个点调用析构函数(我猜是在函数的末尾)。为什么调用析构函数?我该怎么做 注意:这不是我的实际代码,我制作了一个玩具示例,所以不会太长 对象 class Bar { public: Bar(int y) { this->x = y; } ~Bar() {
phoenix::construct
创建一个指向另一个对象的指针的对象。我注意到使用phoenix::construct
在某个点调用析构函数(我猜是在函数的末尾)。为什么调用析构函数?我该怎么做
注意:这不是我的实际代码,我制作了一个玩具示例,所以不会太长
对象
class Bar
{
public:
Bar(int y)
{
this->x = y;
}
~Bar()
{
}
int x;
};
class Foo
{
public:
Foo()
{
}
Foo(Bar* bar) : _bar(bar)
{
}
~Foo()
{
delete this->_bar;
}
void DoStuff()
{
std::cout << this->_bar->x << std::endl;
}
private:
Bar *_bar;
};
类栏
{
公众:
条形图(整数y)
{
这个->x=y;
}
~Bar()
{
}
int x;
};
福班
{
公众:
Foo()
{
}
Foo(Bar*Bar):\u Bar(Bar)
{
}
~Foo()
{
删除此->\u栏;
}
void DoStuff()
{
标准::cout_bar->x
{
TestGrammar():TestGrammar::基本类型(foo)
{
foo=bar[qi::_val=phoenix::construct(qi:_1)];
bar=qi::double_[qi::_val=phoenix::new(qi::_1)];
}
qi::ruleFoo;
qi::ruleBar;
};
呼叫代码
std::getline(std::cin, string);
iter = string.begin();
end = string.end();
bool result = qi::phrase_parse(iter, end, grammar, space, f);
if (result)
{
State s;
f.DoStuff();
}
else
{
std::cout << "No Match!" << std::endl;
}
std::getline(std::cin,string);
iter=string.begin();
end=string.end();
bool result=qi::短语解析(iter,end,grammar,space,f);
如果(结果)
{
s国;
f、 DoStuff();
}
其他的
{
std::cout析构函数在foo
规则的属性上运行;这是在它被复制到引用的属性之后运行的
由于您的Foo
类违反了三条规则,因此会导致问题。Foo
不应可复制(或实现深度复制)
标记如下:
class Foo : public boost::noncopyable {
将显示语法复制了Foo
:
/home/sehe/custom/boost_1_57_0/boost/proto/transform/default.hpp|154 col 9| error: use of deleted function ‘Foo& Foo::operator=(const Foo&)’
简言之:
用零规则或{3 | 5}规则编写卫生类
不要在Spirit语法中进行动态分配,这会给你带来悲伤
不执行语义操作()
下面是一个通过实现三个规则快速修复的版本:
struct Foo {
Foo(Bar *bar = 0) : _bar(bar) {}
Foo(Foo const& o) : _bar(new Bar(*o._bar)) {}
Foo& operator=(Foo const& o) {
Foo tmp;
tmp._bar = new Bar(*o._bar);
std::swap(tmp._bar, _bar);
return *this;
}
~Foo() { delete _bar; }
void DoStuff() { std::cout << _bar->x << std::endl; }
private:
Bar *_bar;
};
struct Foo {
using PBar = boost::shared_ptr<Bar>;
Foo(PBar bar = {}) : _bar(bar) {}
void DoStuff() { std::cout << _bar->x << std::endl; }
private:
PBar _bar;
};
structfoo{
Foo(Bar*Bar=0):\u Bar(Bar){}
Foo(Foo const&o):_bar(新bar(*o._bar)){}
Foo&运算符=(Foo const&o){
Foo-tmp;
tmp.\u条=新条(*o.\u条);
标准:交换(tmp.\u-bar,\u-bar);
归还*这个;
}
~Foo(){delete}
void DoStuff(){std::cout x InDoStuff
别忘了nullptr
-检查\u bar
谢谢你的精彩回答(和往常一样)。我还有很多东西要学,我很感谢你指出一些需要注意的事情。在管理的世界里花费这么多时间毁了我哈哈
struct Foo {
using PBar = boost::shared_ptr<Bar>;
Foo(PBar bar = {}) : _bar(bar) {}
void DoStuff() { std::cout << _bar->x << std::endl; }
private:
PBar _bar;
};
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/make_shared.hpp>
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct Bar {
Bar(int y) { this->x = y; }
~Bar() {}
int x;
};
struct Foo {
using PBar = boost::shared_ptr<Bar>;
Foo(PBar bar = {}) : _bar(bar) {}
void DoStuff() { std::cout << _bar->x << std::endl; }
private:
PBar _bar;
};
template <typename Iterator>
struct TestGrammar : qi::grammar<Iterator, Foo(), ascii::space_type>
{
TestGrammar() : TestGrammar::base_type(foo)
{
using namespace qi;
foo = bar [ _val = phoenix::construct<Foo::PBar>(_1) ] ;
bar = double_ [ _val = phoenix::new_<Bar>(_1) ] ;
}
qi::rule<Iterator, Foo(), ascii::space_type> foo;
qi::rule<Iterator, Bar*(), ascii::space_type> bar;
};
int main() {
std::string input;
std::getline(std::cin, input);
using It = std::string::const_iterator;
It iter = input.begin();
It end = input.end();
TestGrammar<It> grammar;
Foo f;
bool result = qi::phrase_parse(iter, end, grammar, ascii::space, f);
if (result) {
//State s;
f.DoStuff();
} else {
std::cout << "No Match!" << std::endl;
}
}