- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
真的很简单的问题,让我介绍一下背景:
我有一个 mpl::vector
类型,其中每个类型都有一个 id,在运行时我使用 mpl::for_each
遍历这个 vector 并找到给定 id 的匹配类型。但是一旦找到,继续循环就没有意义了,所以 - 问题是,有没有办法跳出它(不抛出异常)?
最佳答案
为了实现类似 find_if
的功能,我更改了 for_each(将其称为 exec_if
)以采用 bool
模板参数。 bool
指示是否应该执行下一个序列,或者影响提前返回。
#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/assert.hpp>
namespace mpl = boost::mpl;
template< bool done = true >
struct exec_if_impl
{
template<typename Iterator, typename LastIterator, typename Pred, typename Exec>
static void execute(Iterator*, LastIterator*, Pred const&, Exec const&)
{
}
};
template<>
struct exec_if_impl<false>
{
template<typename Iterator, typename LastIterator, typename Pred, typename Exec>
static void execute(Iterator*, LastIterator*, Pred const& f, Exec const& e)
{
typedef typename mpl::deref<Iterator>::type item;
if (!f(static_cast<item*>(0)))
{
typedef typename mpl::next<Iterator>::type iter;
exec_if_impl<boost::is_same<iter, LastIterator>::value>
::execute(static_cast<iter*>(0), static_cast<LastIterator*>(0), f, e);
}
else
e(static_cast<item*>(0));
}
};
template<typename Sequence, typename Pred, typename Exec>
inline
void exec_if(Pred const& f, Exec const& e, Sequence* = 0)
{
BOOST_MPL_ASSERT(( mpl::is_sequence<Sequence> ));
typedef typename mpl::begin<Sequence>::type first;
typedef typename mpl::end<Sequence>::type last;
exec_if_impl<boost::is_same<first,last>::value>
::execute(static_cast<first*>(0), static_cast<last*>(0), f, e);
}
namespace msg
{
struct m1 { enum { TYPE = 1 }; static const char* name() { return "m1"; } };
struct m2 { enum { TYPE = 2 }; static const char* name() { return "m2"; } };
struct m3 { enum { TYPE = 3 }; static const char* name() { return "m3"; } };
struct m4 { enum { TYPE = 4 }; static const char* name() { return "m4"; } };
struct m5 { enum { TYPE = 5 }; static const char* name() { return "m5"; } };
}
struct checker
{
checker(int chk_type) : type(chk_type) {}
template <typename Mtype>
bool operator()(Mtype* = 0) const
{
return Mtype::TYPE == type;
}
int type;
};
struct exec
{
template <typename Mtype>
void operator()(Mtype* = 0) const
{
std::cout << Mtype::name() << " executed" << std::endl;
}
};
int main(void)
{
typedef mpl::vector<msg::m1, msg::m2, msg::m3, msg::m4, msg::m5> mseq;
checker chk(3);
exec_if<mseq>(chk, exec());
return 0;
}
我将其更改为 exec_if
,因此现在当谓词匹配时,将使用该类型触发要执行的仿函数 - 这正是我需要的。
关于c++ - 有没有办法突破 boost::mpl for_each?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4798169/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
在现代 IDE 中,有一个键盘快捷键可以通过键入文件名称来打开文件,而无需将手放在鼠标上。例如: Eclipse:Cmd|Ctrl + Shift + R -> 打开资源 IntelliJ:Cmd|C
有什么东西会等待事件发生(我正在等待的是 WebBrowser.DocumentCompleted),然后执行代码吗?像这样: If (WebBrowser.DocumentCompleted) 不会
我使用 PHP Minify,它很棒。但我的问题是,是否有任何 PHP 插件或其他东西可以自动检测 javascript/css 代码并自动缩小它?谢谢。 最佳答案 Javascript 压缩器? 看
有没有一种语言,类似什么CoffeeScript是JavaScript,编译成windows batch|cmd|command line的语言? 我指的cmd版本是基于NT的,尤其是XP sp3及以
我知道我可以 ,但是,我真的宁愿有一个任务,我可以从任何可以使用所有(或至少大部分)属性的操作系统调用 copy ,但这并没有消除 unix 上的权限。 我想知道是否已经有解决方案,或者我必须自己编
我正在使用 Vuejs(不使用 jQuery)开发一个项目,该项目需要像 jvectormap 这样的 map 但正如我所说,我没有使用 jQuery,那么是否有任何其他库可以在不使用 jQuery
想要进行一个简单的民意调查,甚至不需要基于 cookie,我不在乎投了多少票。有没有类似的插件或者简单的东西? 最佳答案 这是一个有用的教程 - 让我知道它是否适合您 using jQuery to
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
var FileBuff: TBytes; Pattern: TBytes; begin FileBuff := filetobytes(filename); Result := Co
我想要一个 vqmod xml 文件来添加一次上传多个图像的功能。身边有这样的事吗? 编辑:Opencart版本:2.1.0.1 最佳答案 最后我写了一个xml来添加到opencart 2.1.0.1
所以考虑这样的函数: public void setTemperature(double newTemperatureValue, TemperatureUnit unit) 其中Temperatur
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我是 ggplot2 的新手,一直在尝试找到一个全面的美学列表。我想我理解它们的目的,但很难知道哪些可以在各种情况下使用(主要是几何图形?)。 Hadley 的网站偶尔会在各个几何图形的页面上列出可用
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
是否有任何 PHP 函数可以将整数转换为十万和千万? 900800 -> 9,00,800 500800 -> 5,00,800 最佳答案 由于您已在问题标签中添加了 Yii,因此您可以按照 Yii
使用 Clojure 一段时间后,我积累了一些关于它的惰性的知识。我知道诸如map之类的常用API是否是惰性的。然而,当我开始使用一个不熟悉的API(例如with-open)时,我仍然感到怀疑。 是否
我的项目需要一个像 AvalonDock 这样的对接系统,但它的最后一次更新似乎是在 2013 年 6 月。是否有更多...积极开发的东西可以代替它? 最佳答案 AvalonDock 实际上相当成熟并
我正在寻找一个可以逆转 clojure 打嗝的函数 所以 turns into [:html] 等等 根据@kotarak的回答,这现在对我有用: (use 'net.cgrand.enliv
我是一名优秀的程序员,十分优秀!