- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理的代码使用递归对。在简化的示例中,该类型包含一对由 string
和 int
或另一对组成的对。这应该(并且已经)可以通过以下方式实现:
#include <boost/variant.hpp>
#include <string>
using namespace std;
typedef boost::make_recursive_variant
< std::pair
< std::string
, boost::variant<int, boost::recursive_variant_>
>
>::type recursivePair;
typedef boost::variant< int
, recursivePair
> intPairVariant;
using sType = boost::variant<pair<string, string>, int>;
void foo(){
sType bar(make_pair("aa", "bb"));
recursivePair xxx(std::make_pair (std::string("s"), intPairVariant()));
recursivePair yyy(std::make_pair (string("s"), 5));
recursivePair zzz(std::make_pair ("s", 5));
}
sType
是为了表明该对中的隐式转换仍然适用。
但是从 Boost 1.62 开始,编译过程中会中断:
error: no matching function for call to ‘boost::variant<boost::detail::variant::recursive_flag<std::pair<std::__cxx11::basic_string<char>, boost::variant<int, boost::recursive_variant_> > > >::variant(std::pair<std::__cxx11::basic_string<char>, int>)’
recursivePair yyy(std::make_pair (string("s"), 5));
这仅适用于一种类型。其他人也同样失败。
有谁知道为什么这不再有效以及如何解决这个问题?
最佳答案
我认为您实际上遇到了与 std::pair<>
类似的更改在这里看到:does boost-1.55 boost::property_tree::ptree compile with c++11?
自 c++11 以来,std::pair 的隐式转换较少。的确,您的代码确实编译了 boost < 1.62,但从本质上讲,这看起来是一个错误,至少在 c++11 模式下是这样。
在 C++11 中,你这样做:
std::make_pair(s, i); // s is std::string, i is int
结果为 std::pair<std::string, int>
.接下来你不仅要求隐式转换 std::pair<std::string, int>
至 std::pair<std::string, IntPairVariant>
,但您希望使用该转换的结果作为您正在分配的变体的初始值设定项。
在 C++ 的所有部分中,这都要求进行两次隐式转换,编译器永远不会使用它来解析重载。
因此,实际上您的代码有点草率,因为它使用了 Boost Variant 可能不应该有的“回旋余地”。它是一个重大变化,但新行为似乎更有意义。
您正在使用单个元素制作递归变体。这……有点奇怪。
这通过隐藏 std::pair<>
对类型系统造成了不必要的“压力”。第一层下的结构特性variant
.
std::pair
直接我能想到的最无聊的事:
#include <boost/variant.hpp>
#include <string>
using namespace std;
typedef std::pair<std::string,
boost::make_recursive_variant<int, std::pair<std::string, boost::recursive_variant_> >::type >
recursivePair;
typedef boost::variant<int, recursivePair> intPairVariant;
int main() {
recursivePair xxx(std::string("s"), intPairVariant());
recursivePair yyy(string("s"), 5);
recursivePair zzz("s", 5);
}
请注意,这已经允许您问题中的确切拼写:
recursivePair xxx(std::make_pair(std::string("s"), intPairVariant()));
recursivePair yyy(std::make_pair(string("s"), 5));
recursivePair zzz(std::make_pair("s", 5));
但是make_pair
在这三种情况下都是多余的。
也许你可以做些更像是
struct V;
using Pair = std::pair<std::string, V>;
struct V : boost::variant<int, boost::recursive_wrapper<Pair> > {
using base = boost::variant<int, boost::recursive_wrapper<Pair> >;
using base::base;
using base::operator=;
};
现在你可以放心地说
Pair xxx("s", V{});
Pair yyy("s", 5);
Pair zzz{};
使用派生结构而不是普通变体的好处是您实际上可以消除构造函数的歧义:
#include <boost/variant.hpp>
#include <string>
#include <iostream>
namespace mylib {
struct V;
using Pair = std::pair<std::string, V>;
struct V : boost::variant<int, boost::recursive_wrapper<Pair> > {
using base = boost::variant<int, boost::recursive_wrapper<Pair> >;
V() = default;
V(V&&) = default;
V(V const&) = default;
V& operator=(V&&) = default;
V& operator=(V const&) = default;
V(int i) : base(i) {}
V(std::string const& key, V value);
};
V::V(std::string const& key, V value) : base{Pair{std::move(key), std::move(value)}} {}
static inline std::ostream& operator<<(std::ostream& os, Pair const& p) {
return os << "{" << p.first << "," << p.second << "}";
}
}
int main() {
using mylib::V;
V xxx("s", mylib::V{});
V yyy("s", 5);
V zzz{};
V huh("s", {"more", {"intricate", {"nested", { "truths", 42} } } });
V simple = 5;
simple = {"simple", 5};
simple = {"not_so_simple", simple};
std::cout << "xxx:" << xxx << "\n";
std::cout << "yyy:" << yyy << "\n";
std::cout << "zzz:" << zzz << "\n";
std::cout << "huh:" << huh << "\n";
std::cout << "simple:" << simple << "\n";
}
打印
xxx:{s,0}
yyy:{s,5}
zzz:0
huh:{s,{more,{intricate,{nested,{truths,42}}}}}
simple:{not_so_simple,{simple,5}}
关于c++ - 通过 boost 的递归对。自 boost 1.62 以来变体已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49496103/
我正在创建一个连接到 firebase 的应用程序。但我面临一些问题。当同步我的 gradle 文件时,我收到此警告 WARNING: API 'variant.getMergeResources()
想知道是否有任何方法可以将变体分配给自定义 radio 输入?我想为 2 天、3 天和标准运输设置不同费率的分级运输。我可以使用变体来做到这一点,但下拉菜单对我不起作用。我想要日期信息和日期选择器,以
我是 Haskell 的新手。鉴于 Haskell 的整个前提是函数将始终返回相同的值,我希望有某种方式,例如在编译时计算常量的斐波那契值,就像我可以在 C++ 中使用模板元编程一样,但我不知道该怎么
我是 OCaml 的新手,但过去两天一直在工作,以便更好地了解如何使用它。我最近做了很多事情,但有些事情阻碍了我前进。 我正在尝试在 OCaml 中实现 evaexpr。使用这种语言非常容易,你会说:
我有一个使用一些typedef的std::variant的代码库。 最初,它们是不同的类型,但现在它们像下面的示例一样重叠 typedef int TA; typedef int TB; std::v
鉴于此: data Foo = Bar { name :: String } | Baz { nickname :: String } 函数 name 和 nickname 似乎都是 Foo -> S
嘿,我猜这可能相当微不足道,但我很难找到答案或弄清楚它。 我正在尝试创建一个带有任意间距的彩色方 block 网格。这本身很容易做到,特别是因为我只需要九个正方形。但是虽然我看着我完成的代码,我不禁觉
我有 woocommerce 设置,其中包含产品和这些产品的变体。当我更改变化时(例如,产品的尺寸(340克或900克),我希望它在页面上动态更新价格,以便人们可以看到两种尺寸之间的价格差异。目前,我
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
在我的一节课上,我被问到这是一个脑筋急转弯,但我无法弄明白(这不是家庭作业问题,只是其中一位助教给我们的一个脑筋急转弯让我们思考)。 给你一根杆,上面有 n 个要切割的点,例如 [1,5,11],以及
关于 CRP如果我想实现它的细微变化(使用模板模板参数),我会得到一个编译错误: template class Derived> class Base { public: void Call
我正在创建一个 woocommerce 主题,并且我有产品变体,即显示在产品详细信息页面上的尺寸,但问题是我想通过使用产品 ID 在我的自定义 php 页面中获取所有变体,任何人都可以帮助我。 提前致
我正在使用 Ionic 开发移动应用程序,我必须与 Twitter API 连接。 所以我使用 ng-cordova 和 $cordovaAuth .但是当我这样做时: $cordovaOauth.t
这里的网站有一个音乐播放器http://www.numberonedesigns.com/ubermusic.com/ ...当点击下一个按钮并随机突出显示时,它不会正确随机化。它总是返回到播放列表中
我有列 sql 变体,其含义如下:100, 150, D1我正在尝试根据特定逻辑将列中的所有数字转换为字母(例如 D1)以防万一。但是 150 有空格并且 CASE WHEN 不起作用。 这是我正在使
有没有一种快速方法可以用从匹配模式派生的数据替换所有出现的某些模式? 例如,如果我想将字符串中出现的所有数字替换为用 0 填充到固定长度的相同数字。 在本例中,如果长度为 4,则 ab3cd5 将变为
我目前正在寻找生成具有特定位数的数字列表,我的代码当前如下: | Python 2.7 | import itertools inp = raw_input('Number of digits to
我正在对类型系统进行研究。对于这项工作,我正在研究流行语言中变体、结构子类型、通用多态性和存在多态性的用法。像 heskell、ocaml 这样的功能性语言提供了这样的功能。但我想知道像 C++ 这样
这是 variant.hpp 文件中的相关代码(可在此处找到 http://www.boost.org/doc/libs/1_49_0/boost/variant/variant.hpp) templ
您好,我有两个具有多对多关系的表和一个联结表。简而言之,具有不同属性的产品也随之增加了价格。为了更清楚地说明我是产品和属性表。 +-------------+------------+--------
我是一名优秀的程序员,十分优秀!