正如所评论的那样,我认为解决这个难题的最简洁的方法是boost::variant<>
使用允许客户覆盖外部使用行为的运营商策略(实际上是每个运营商)来增强实施。(显然这是很多通用编程工作)。
我已经实施了一种解决方法。这使您可以为变体实现自定义运算符,即使它已在boost/variant.hpp
.
我的脑电波是使用BOOST_STRONG_TYPEDEF
.
这个想法是通过使我们的变体具有不同的实际类型来打破重载解决方案(或至少使我们的自定义重载成为首选解决方案)(它有点让人想起“绝望”的 ADL 障碍:你不能从范围中隐藏using
名称,并且您不能进入“非军事化名称空间”(障碍),因为冲突的声明位于类名称空间本身中;但是您可以使它们不适用于您的“诱饵”类型)。
唉,这对和家庭来说效果不佳operator<
,因为 boost strong-typedef 实际上很难使用“基本”类型来保留(弱)总排序语义。在普通英语中:强类型定义operator<
也定义(委托给基类型的实现)。
不用担心,我们可以做一个 CUSTOM_STRONG_TYPEDEF 并继续我们的快乐方式。查看 main 中的测试用例以进行概念证明(下面的输出)。
由于所描述的有趣的交互,我选择operator<
了这个演示,但我想你不会有任何事情operator==
可以为你的变体类型定制。
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
/////////////////////////////////////////////////////
// copied and reduced from boost/strong_typedef.hpp
#define CUSTOM_STRONG_TYPEDEF(T, D) \
struct D \
/*: boost::totally_ordered1< D */ \
/*, boost::totally_ordered2< D, T */ \
/*> > */ \
{ \
T t; \
explicit D(const T t_) : t(t_) {}; \
D(){}; \
D(const D & t_) : t(t_.t){} \
D & operator=(const D & rhs) { t = rhs.t; return *this;} \
D & operator=(const T & rhs) { t = rhs; return *this;} \
operator const T & () const {return t; } \
operator T & () { return t; } \
/*bool operator==(const D & rhs) const { return t == rhs.t; } */\
/*bool operator<(const D & rhs) const { return t < rhs.t; } */\
};
namespace detail
{
typedef boost::variant<unsigned int, std::string> variant_t;
struct less_visitor : boost::static_visitor<bool>
{
bool operator()(const std::string& a, int b) const
{ return boost::lexical_cast<int>(a) < b; }
bool operator()(int a, const std::string& b) const
{ return a < boost::lexical_cast<int>(b); }
template <typename T>
bool operator()(const T& a, const T& b) const
{ return a < b; }
};
struct variant_less
{
less_visitor _helper;
bool operator()(const variant_t& a, const variant_t& b) const
{ return boost::apply_visitor(_helper, a, b); }
};
}
CUSTOM_STRONG_TYPEDEF(detail::variant_t, custom_vt);
namespace
{
bool operator<(const custom_vt& a, const custom_vt& b)
{ return detail::variant_less()(a, b); }
std::ostream& operator<<(std::ostream& os, const custom_vt& v)
{ return os << (const detail::variant_t&)v; }
}
int main()
{
const detail::variant_t I(43), S("42");
const custom_vt i(I), s(S);
// regression test (compare to boost behaviour)
std::cout << "boost: " << I << " < " << S << ": " << std::boolalpha << (I<S) << "\n";
std::cout << "boost: " << S << " < " << I << ": " << std::boolalpha << (S<I) << "\n";
// FIX1: clumsy syntax (works for boost native variants)
detail::variant_less pred;
std::cout << "clumsy: " << i << " < " << s << ": " << std::boolalpha << pred(i,s) << "\n";
std::cout << "clumsy: " << s << " < " << i << ": " << std::boolalpha << pred(s,i) << "\n";
std::cout << "clumsy: " << I << " < " << S << ": " << std::boolalpha << pred(I,S) << "\n";
std::cout << "clumsy: " << S << " < " << I << ": " << std::boolalpha << pred(S,I) << "\n";
// FIX2: neat syntax (requires a custom type wrapper)
std::cout << "custom: " << i << " < " << s << ": " << std::boolalpha << (i<s) << "\n";
std::cout << "custom: " << s << " < " << i << ": " << std::boolalpha << (s<i) << "\n";
}
输出:
boost: 43 < 42: true
boost: 42 < 43: false
clumsy: 43 < 42: false
clumsy: 42 < 43: true
clumsy: 43 < 42: false
clumsy: 42 < 43: true
custom: 43 < 42: false
custom: 42 < 43: true
当然,如果您想将 custom_vt 传递到使用 TMP 作用于变体的库 API 中,则可能会出现不幸的交互。但是,由于两者之间的转换很轻松,您应该能够通过在适当的时间使用 detail::variant_t 来“争取自己的出路”。
这是您在呼叫站点获得语法便利所必须付出的代价。