自 boost 1.62 以来,通过 boost.Variant 的递归对已损坏
Recursive pair via boost.Variant broken since boost 1.62
我正在处理的代码使用递归对。在简化的示例中,类型包含一对由 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 的隐式转换较少。确实,您的代码 did 编译了 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++ 的所有部分中,这都要求进行两次隐式转换,编译器永远不会使用它来解析重载。
因此,实际上您的代码有点草率,因为它使用了 "leeway",而 Boost Variant 可能不应该有。它是一个重大变化,但新行为似乎更有意义。
另一个笔记
您正在使用单个元素制作递归变体。这……有点奇怪。
通过将 std::pair<>
结构 属性 隐藏在 variant
的第一层下,"strains" 类型系统有点多余。
直接使用 A 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}}
我正在处理的代码使用递归对。在简化的示例中,类型包含一对由 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 的隐式转换较少。确实,您的代码 did 编译了 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++ 的所有部分中,这都要求进行两次隐式转换,编译器永远不会使用它来解析重载。
因此,实际上您的代码有点草率,因为它使用了 "leeway",而 Boost Variant 可能不应该有。它是一个重大变化,但新行为似乎更有意义。
另一个笔记
您正在使用单个元素制作递归变体。这……有点奇怪。
通过将 std::pair<>
结构 属性 隐藏在 variant
的第一层下,"strains" 类型系统有点多余。
直接使用 A 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}}