简体   繁体   English

使用zip迭代器调用boost :: compute :: sort()会产生构建错误

[英]Call of boost::compute::sort() with zip iterators delivers build errors

I have a problem building a program that makes use of the Boost.Compute library. 我在构建一个使用Boost.Compute库的程序时遇到了问题。 I use two zip_iterator s consisting out of a tuple of two float iterators each to sort two float-vectors with the boost::compute::sort() function. 我使用两个zip_iterator由两个float迭代器组成,每个浮点迭代zip_iterator boost::compute::sort()函数对两个float-vector进行boost::compute::sort() My code (both compute::vector s have been filled with float values before): 我的代码( compute::vector s之前都填充了浮点值):

typedef compute::vector<float>::iterator Float_Iterator;
typedef boost::tuple<Float_Iterator, Float_Iterator> Sort_Tuple;
typedef compute::zip_iterator<Sort_Tuple> Sort_Iterator;

Sort_Iterator first_sort = compute::make_zip_iterator
(boost::make_tuple(d_x.begin(), d_y.begin()));
Sort_Iterator last_sort = compute::make_zip_iterator
(boost::make_tuple(d_x.end(), d_y.end()));

BOOST_COMPUTE_FUNCTION(bool, compare, (const boost::tuple<float, float> p1, const boost::tuple<float, float> p2),
{
    return boost_tuple_get(p1, 0) < boost_tuple_get(p2, 0);
}
);

compute::sort(first_sort, last_sort, compare, queue);

When compiling this, I receive: 编译时,我收到:

error C2782: 'void   boost::compute::detail::dispatch_merge_blocks(Iterator,Iterator,Compare,size_t,c onst size_t,const size_t,const size_t,boost::compute::command_queue &)' :   **template parameter 'Iterator' is ambiguous**
          c:\local\boost_1_62_0\boost\compute\algorithm\detail\merge_sort_on_cpu.hpp(129)     : see declaration of 'boost::compute::detail::dispatch_merge_blocks'
          could be **'Sort_Iterator'
 or       'boost::compute::buffer_iterator<T>'**
          with
          [
              T=value_type
          ]

As you can see in my code, I call the function with two Sort_Iterator s I have declared before. 正如您在我的代码中看到的那样,我使用之前声明过的两个Sort_Iterator调用该函数。 Both arguments have the same type, so why should the compiler assume any ambiguity? 两个参数都有相同的类型,那么编译器为什么要假设任何歧义呢? I don't get this. 我不懂。

However, if I try to explicitly type cast the function arguments, like 但是,如果我尝试显式地键入强制转换函数参数,比如

compute::sort((Sort_Iterator)first_sort, (Sort_Iterator)last_sort, compare,    queue);

the latter part of the error message changes to: 错误消息的后半部分更改为:

could be **'boost::compute::zip_iterator<Sort_Tuple>'
or       'boost::compute::buffer_iterator<T>'**
with
[
     T=value_type
]

Even if your code compiled, you wouldn't be able to sort zip iterators: they are read-only. 即使您的代码已编译,您也无法对zip迭代器进行排序:它们是只读的。 You must sort a vector of tuples instead. 您必须改为排序元组向量。

Regarding the Iterator ambiguity, the compiler isn't referring to the types you are passing, but to the Iterator of some function much deeper in the call chain: 关于Iterator歧义,编译器不是指传递的类型,而是指代调用链中更深层次函数的Iterator

template<class Iterator, class Compare>
inline void dispatch_merge_blocks(Iterator first, Iterator result, /* ... /);

It is called this way: 它被称为这样:

dispatch_merge_blocks(first, temp.begin(), /* ... */);
dispatch_merge_blocks(temp.begin(), first, /* ... */);

Here, first is the Iterator you passed in, of type: 这里, first是你传入的Iterator ,类型为:

zip_iterator<tuple<buffer_iterator<float>, buffer_iterator<float>>>

And temp.begin() is... temp.begin()是......

typedef typename std::iterator_traits<Iterator>::value_type value_type;

// temporary buffer for merge result
vector<value_type> temp(count, context);

a vector<tuple<float, float>>::iterator (1) . vector<tuple<float, float>>::iterator (1) The compiler sees two different types for the same template parameter, and hence deduction fails. 编译器为同一模板参数看到两种不同的类型,因此演绎失败。

Additionally, further in the call chain appears the following function: 此外,在调用链中还会出现以下功能:

template<class Iterator, class Compare>
inline void merge_blocks(Iterator first, Iterator result, /* ... */)
{
    // dummy iterator as it's not sort by key
    Iterator dummy;
    merge_blocks(first, dummy, result, dummy, /* ... */);
}

Note the line: Iterator dummy; 注意这一行: Iterator dummy; . That fails to compile because zip_iterator has no default constructor, so template instantiation fails and the whole thing fails to compile. 由于zip_iterator没有默认构造函数,因此无法编译,因此模板实例化失败并且整个程序无法编译。

(1) : I am not entirely sure how value_type ends up being deduced as tuple<float, float> , and going through the endless layers of templates is giving me a mild headache, but this is what is happening, essentially. (1) :我不完全确定value_type最终被推断为tuple<float, float> ,并且通过无休止的模板层给我一个轻微的头痛,但这正是发生的事情,本质上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM