范围可用于切片Boost多维数组(multi_array)。根据{{3}},有几种方法可以定义范围,但并非所有方法都可以编译。我在Ubuntu 11.04上使用GCC 4.5.2。
#include <boost/multi_array.hpp>
int main() {
typedef boost::multi_array_types::index_range range;
range a_range;
// indices i where 3 <= i
// Does compile
a_range = range().start(3);
// Does not compile
a_range = 3 <= range();
a_range = 2 < range();
return 0;
}
编译器输出为:
ma.cpp: In function ‘int main()’:
ma.cpp:9:26: error: no match for ‘operator<=’ in ‘3 <= boost::detail::multi_array::index_range<long int, long unsigned int>()’
ma.cpp:10:25: error: no match for ‘operator<’ in ‘2 < boost::detail::multi_array::index_range<long int, long unsigned int>()’
我知道如何编译这个,或者缺少什么?
答案 0 :(得分:5)
此处调用的operator<
和operator<=
是模板;因此,为Index
参数提供给所述运算符的值必须与所提供范围的Index
模板参数完全相同。
boost::multi_array_types::index_range::index
类型最终归结为std::ptrdiff_t
的typedef;鉴于您提供int
文字,显然是针对您的平台/配置,std::ptrdiff_t
是除int
以外的某种类型的typedef(根据您的错误消息long
)。
便携式修复是将文字强制转换为正确的类型:
#include <boost/multi_array.hpp>
int main()
{
typedef boost::multi_array_types::index_range range;
typedef range::index index;
range a_range;
a_range = index(3) <= range();
a_range = index(2) < range();
index i(1);
a_range = i <= range();
}