- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个
boost::variant<std::string, int> myVariant;
在这个对象中,我从数据库中保存数据,通常是整数或文本,但有时是作为文本存储在数据库中的时间。所以我想知道我是否可以创建一个访问者,当访问带有字符串的变体对象时,返回一个类型为“tm”的结构。类似的东西:
class timeVisitor : public boost::static_visitor<boost::shared_ptr<tm> >
{
public:
boost::shared_ptr<tm> operator()(string &str) const
{
boost::shared_ptr<tm> dst(new tm());
strptime(str.c_str(), "%Y-%m-%d", dst.get());
return dst;
}
};
然后为了使用它:
boost::shared_ptr<tm> result = boost::apply_visitor( timeVisitor(), myVariant );
问题是,我不想在访问者中创建 tm 结构并弄乱一些共享指针和东西。我更喜欢将已经创建的一个提供给访问者,并在内部进行初始化。像(在使用意义上):
tm result;
int returnCode = boost::apply_visitor( timeVisitor(result), myVariant );
访问者将仅使用 strptime my result tm 结构进行初始化,如果转换为 returnCode 时出现问题,甚至会返回。有谁知道这是如何实现的?我能否以某种方式定义带有两个参数的访问者……或者其他东西?
最佳答案
您的简单示例调用应该有效。向获取引用并存储它的访问者添加一个构造函数,例如:
tm* target;
timeVisitor( tm& a ) : target(&a) {}
int operator()(string &str) const {
strptime(str.c_str(), "%Y-%m-%d", target);
}
关于c++ - 使用 boost visitor 在类型之间进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7915837/
在维基百科中 sample在 GoF 书中,访问者模式的使用是通过调用某些接受器上的 accept 方法开始的。但是为什么会这样呢?为什么我们不能以所需的接受器作为参数开始调用 visit 方法?我们
我正在尝试制作antrl4 4.6。工作,但我收到 4 个高级错误,我无法解决它们。有人可以帮我吗?也许需要匿名类。 还有 super() 错误,我将它放在正确的位置,但它拒绝工作。 Yoco.jav
跟踪特定页面的页面浏览量(特别是独特的)的最佳方法是什么? 示例:论坛中的话题、视频网站中的视频、问答脚本 (SO) 中的问题。 目前,我正在尝试为每一行采用一个简单的“浏览量”列来计算浏览量,但是,
在我对 Rust 的有限理解中,我认为 trait 就像接口(interface)——当你实现一个接口(interface)时,你需要实现所有的方法。 我现在正在编写自定义 serde::Deseri
我正在做一个关于访问者设计模式的演示,我了解它是如何工作的,但我还没有找到“定义的”优点和缺点,而且我不想自己推测优点或缺点,因为我可以设置虚假信息。 最佳答案 以下是我对 Visitor 的一些想法
我正在做一个关于访问者设计模式的演示,我了解它是如何工作的,但我还没有找到“定义的”优点和缺点,而且我不想自己推测优点或缺点,因为我可以设置虚假信息。 最佳答案 以下是我对 Visitor 的一些想法
访问者模式表示一个作用于某对象结构中各元素的操作。它可以在不修改各元素类的前提下定义作用于这些元素的新操作,即动态的增加具体访问者角色。 访问者模式利用了双重分派。先将访问者传入元素对象的Accep
代码如下: <?php /** * 访问者模式 * * 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这
访问者模式(Visitor Pattern)使用了一个访问者类,它改变了元素类的执行算法,通过这种方式,元素的执行算法可以随着访问者改变而改变 访问者模式中,元素对象已接受访问者对象,这样访问者对象
我尝试使用三种不同的方法提取特定目录的(唯一)访问者计数: * 有简介 * 使用动态高级段 * 使用自定义报告过滤器 在较小的站点上,这三种方法给出相同的结果。但是在大型网站(> 500 万次访问/月
我使用 boost::variant 制作了一个程序,但不知何故 const 不正确。 错误:将“const CompareTitle”作为“bool CompareTitle::operator()
我正在开发一个应用程序的设计,我想我可能会应用某种 Visitor design pattern ,但事实证明这并不是我要找的。也许有人可以指出我在这种情况下需要的变体? 我的大部分代码都有一个模板参
我正在寻找一种简洁的设计来模拟 Visitor 的功能,而没有它的许多缺点。在 Java 中,传统的实现(如 GoF 中所述)采用双重分派(dispatch)来摆脱 if-elses。为了解决这个问题
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我正在尝试编写一个简单的脚本系统(用于视频游戏),其中没有非常复杂的任务,只有简单的 bool 检查和操作集。 我是一名学生,但我不是在学习编程,我只是把它作为一种爱好,所以我正在学习基础知识,最近我
我使用类似 STL 的迭代器在 C++ 中实现了访问者模式,用于存储访问者在容器中的当前位置。现在我想在迭代容器时更改容器,我特别感兴趣的是从容器中删除项目,即使是我当前正在访问的项目。 现在显然这将
我正在尝试以参数化方式实现访问者模式的修改版本,以这种方式避免“通用访问者”对每个具体元素都有过载,但是,由于我没有太多经验在模板编程中,我不知道如何完成“模式”。 代码: // test.cpp #
我没有看到有效的示例,而且我看到一些拉取请求仍处于打开状态。我想避免沿着花园小径走。 https://github.com/antlr/antlr4/pull/1807 https://github.
双重分发(double dispatch) 什么是双重分发? 谈起面向对象的程序设计时,常说起的面向对象的「多态」,其中关于多态,经常有一个说法是「父类引用指向子类对象」。 这种父类的引用指向
本文整理了Java中org.raml.parser.visitor.YamlValidator类的一些代码示例,展示了YamlValidator类的具体用法。这些代码示例主要来源于Github/Sta
我是一名优秀的程序员,十分优秀!