c++ - Boost ICL,区间集的基数

标签 c++ boost boost-icl

在 Boost ICL 中,当我在区间集上调用 cardinality() 或 size() 函数时,返回类型为 size_t,与区间类型无关。在 32 位机器上,这是一个 32 位无符号整数。但是,如果我的间隔是 int64_t 类型,则基数很容易溢出 32 位整数。我在这里遗漏了一些明显的东西还是这个库的严重缺陷?

编辑:添加示例

以下代码在 64 位机器上编译和运行没有错误,但在 32 位机器上没有,它抛出断言。

#include <boost/icl/interval_set.hpp>

int main()
{
    boost::icl::interval_set<int64_t> is;
    is.add(boost::icl::interval<int64_t>::closed(1, 4294967297LL));
    assert(boost::icl::cardinality(is) == 4294967297LL);
}

编辑:我在 Ubuntu 13.10 上使用 boost::icl 版本 1.49.0

编辑:

这不是一个特别的 32/64 位问题,因为下面的代码在 64 位上也不起作用

#include <boost/icl/interval_set.hpp>
int main()
{
    boost::icl::interval_set<double> is;
    is.add(boost::icl::interval<double>::closed(1, 1.5));
    assert(boost::icl::cardinality(is) == 0.5);
}

最佳答案

转载于 Ubuntu 14.04.1 LTS 上的 Boost 1_54

这似乎确实是一个错误。要修复的特化是

template <class Type> 
struct get_size_type<Type, false, false, false>
{ 
    typedef std::size_t type; 
};

icl/type_traits/size_type_of.hpp .不知何故,这些天 ICL 开发人员似乎没有使用 -m32 进行测试。

我已经成功地将它替换为

// BEGIN SEHE WAS HERE
template <class Type> 
struct get_size_type<Type, std::enable_if<not boost::is_arithmetic<Type>::value, mpl::false_>::type::value, false, false>
{ 
    typedef std::size_t type; 
};

template <class Type> 
struct get_size_type<Type, std::enable_if<boost::is_arithmetic<Type>::value, mpl::false_>::type::value, false, false>
{ 
    typedef typename std::common_type<Type, std::size_t>::type type; 
};
// END SEHE WAS HERE

可悲的是,该特征对 SFINAE 不是很友好,因此黑客使用第一个 bool SFINAE 的模板参数。改进可能是:

  • 使用boost仅类型特征
  • 使用 Boost Integer 的整数值扣除而不是 common_type<...>对于整数类型

我已经针对 interval_set<double> 对 DoTheRightThing(TM) 进行了测试以及interval_set<uint64_t>在 g++ -m32 和 -m64 上。


我会在邮件列表中报告此事。

关于c++ - Boost ICL,区间集的基数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27224896/

相关文章:

c++ - 带有 gmp 的代码块,带有 << 运算符和 mp*_class 的段错误

c++ - boost rtree.bounds() : getting more boxes and/or accessing to its structure

c++ - 尝试为枚举重载 operator+= 时出现编译器错误

c++ - 使用 C++ Boost 间隔容器库 (ICL) 时如何移动间隔?

c++ - 在 Ubuntu gcc 中从包含特殊字符的 char* 中提取 int

c++ - 数组运算符 [] 重载 const 和非常量版本

c++ - OSX Lion 上的 makefile 中的 GLFW 链接问题

c++ - boost::transform 与 std::transform

C++11 可重入类锁定策略

c++ - 传递 boost discrete_interval 作为此参数会丢弃限定符 [-fpermissive]