Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost multi_array range compilation

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?

like image 574
YXD Avatar asked Jul 07 '11 14:07

YXD


1 Answers

The operator< and operator<= being invoked here are templates; consequently, the value supplied to said operators for the Index argument must be the exact same type as the Index template parameter of the range being supplied.

The boost::multi_array_types::index_range::index type ultimately boils down to a typedef for std::ptrdiff_t; given that you're supplying int literals, clearly for your platform/configuration, std::ptrdiff_t is a typedef for some type other than int (according to your error messages it's long).

The portable fix is to coerce your literals to the proper type:

#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();
}
like image 182
ildjarn Avatar answered Sep 22 '22 13:09

ildjarn