给定一个 boost::tuple 和 std::tuple,你如何在它们之间进行转换?
也就是说,您将如何实现以下两个功能?
template <typename... T> boost::tuple<T...> asBoostTuple( std::tuple<T...> stdTuple);
template <typename... T> std::tuple<T...> asStdTuple (boost::tuple<T...> boostTuple);
看起来很简单,但我找不到任何好的解决方案。
我尝试了什么?
我最终用模板编程解决了这个问题。它似乎适用于我的具体设置,但感觉不对(太冗长),我什至不确定它是否真的适用于所有情况(我稍后会谈到这一点)。
无论如何,这是有趣的部分:
template<int offset, typename... T>
struct CopyStdTupleHelper
{
static void copy(boost::tuple<T...> source, std::tuple<T...>& target) {
std::get<offset>(target) = std::move(boost::get<offset>(source));
CopyStdTupleHelper<offset - 1, T...>::copy(source, target);
}
static void copy(std::tuple<T...> source, boost::tuple<T...>& target) {
boost::get<offset>(target) = std::move(std::get<offset>(source));
CopyStdTupleHelper<offset - 1, T...>::copy(source, target);
}
};
template<typename... T>
struct CopyStdTupleHelper<-1, T...>
{
static void copy(boost::tuple<T...> source, std::tuple<T...>& target)
{ /* nothing to do (end of recursion) */ }
static void copy(std::tuple<T...> source, boost::tuple<T...>& target)
{ /* nothing to do (end of recursion) */ }
};
std::tuple<T...> asStdTuple(boost::tuple<T...> boostTuple) {
std::tuple<T...> result;
CopyStdTupleHelper<sizeof...(T) - 1, T...>::copy(std::move(boostTuple), result);
return result;
}
boost::tuple<T...> asBoostTuple(std::tuple<T...> stdTuple) {
boost::tuple<T...> result;
CopyStdTupleHelper<sizeof...(T) - 1, T...>::copy(std::move(stdTuple), result);
return result;
}
不知道有没有更优雅的方式。将使用 boost::tuple 的现有 API 包装到 std::tuple 似乎是一个非常常见的操作。
我试图为您提供一个最小的测试示例,其中仅缺少 asBoostTuple 和 asStdTuple 的实现。但是,对于 boost::tuples::null_type 的一些魔法,我并不完全理解,它无法编译。这也是我不确定我现有的解决方案是否可以普遍应用的原因。
这是片段:
#include <tuple>
#include <boost/tuple/tuple.hpp>
template <typename... T>
boost::tuple<T...> asBoostTuple(std::tuple<T...> stdTuple) {
boost::tuple<T...> result;
// TODO: ...
return result;
}
template <typename... T>
std::tuple<T...> asStdTuple(boost::tuple<T...> boostTuple) {
std::tuple<T...> result;
// TODO: ...
return result;
}
int main() {
boost::tuple<std::string, int, char> x = asBoostTuple(std::make_tuple("A", 1, 'x'));
// ERROR:
std::tuple<std::string, int, char> y = asStdTuple<std::string, int, char>(x);
return x == y;
}
错误信息是:
example.cpp:20:38: error: no viable conversion from 'tuple<[3 *
...], boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type>' to 'tuple<[3 * ...], (no
argument), (no argument), (no argument), (no argument),
(no argument), (no argument), (no argument)>'
...int, char> y = asStdTuple<std::string, int, char>(x);
我知道 Boost 的实现不是基于可变参数模板,它应该解释 null_type,但我仍然不确定如何避免该编译错误。
最佳答案
进行此类操作时的常用技巧是构造一个整数序列,然后使用包扩展来初始化新元组。
在这种情况下额外的变化是 null_type。为此,可能最简单的做法是将元组类型视为不透明,并使用 boost::tuples::length 和 boost::tuples::element 对其进行操作,后者已经处理null_type 正确。这样您就不会依赖 boost 元组的实现细节。
所以:
template <typename StdTuple, std::size_t... Is>
auto asBoostTuple(StdTuple&& stdTuple, std::index_sequence<Is...>) {
return boost::tuple<std::tuple_element_t<Is, std::decay_t<StdTuple>>...>
(std::get<Is>(std::forward<StdTuple>(stdTuple))...);
}
template <typename BoostTuple, std::size_t... Is>
auto asStdTuple(BoostTuple&& boostTuple, std::index_sequence<Is...>) {
return std::tuple<typename boost::tuples::element<Is, std::decay_t<BoostTuple>>::type...>
(boost::get<Is>(std::forward<BoostTuple>(boostTuple))...);
}
template <typename StdTuple>
auto asBoostTuple(StdTuple&& stdTuple) {
return asBoostTuple(std::forward<StdTuple>(stdTuple),
std::make_index_sequence<std::tuple_size<std::decay_t<StdTuple>>::value>());
}
template <typename BoostTuple>
auto asStdTuple(BoostTuple&& boostTuple) {
return asStdTuple(std::forward<BoostTuple>(boostTuple),
std::make_index_sequence<boost::tuples::length<std::decay_t<BoostTuple>>::value>());
}
Demo .
请注意,对于这两种情况,代码的基本结构完全相同:我们获得元组的大小(通过 boost::tuples::length 或 std::tuple_size ),使用std::make_index_sequence构造一个整数序列,然后使用整数序列获取带有boost::tuples::element和std::tuple_element,以及 boost::get/std::get 的值。
关于c++ - std::tuple 和 boost::tuple 之间的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223786/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。