5

A range can be used to slice a Boost Multidimensional array (multi_array). According to the documentation there are several ways of defining a range, however not all of them will compile. I'm using GCC 4.5.2 on Ubuntu 11.04.

#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;
}

The compiler output is:

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>()’

Any idea how I can compile this, or what is missing?

4

1 に答える 1

5

ここoperator<operator<=呼び出される と はテンプレートです。したがって、引数として前述の演算子に提供される値は、提供される範囲のテンプレート パラメーターとIndexまったく同じ型である必要があります。Index

型はboost::multi_array_types::index_range::index最終的に typedef for に要約されstd::ptrdiff_tます。intプラットフォーム/構成に対して明らかにリテラルを提供していることを考えると、 (エラーメッセージによると)std::ptrdiff_t以外の型のtypedefです。intlong

移植可能な修正は、リテラルを適切な型に強制することです。

#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();
}
于 2011-07-07T16:49:27.050 に答える