我使用的TBB库如下:
// Concurrency.hpp
#include <tbb/spin_mutex.h>
#include <tbb/mutex.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
// Restrict templates to work for only the specified set of types
template<class T, class O = T>
using IntegerOnly = std::enable_if_t<std::is_integral<T>::value, O>;
// An extra helper template
template<class Fn, class I>
static IntegerOnly<I, void> loop_(const tbb::blocked_range<I> &range, Fn &&fn)
{
for (I i = range.begin(); i < range.end(); ++i) fn(i);
}
// Calling TBB parallel-for by this template
template<class It, class Fn>
static void for_each(It from, It to, Fn &&fn, size_t granularity = 1)
{
tbb::parallel_for(tbb::blocked_range{from, to, granularity}, // => Error happens at this line
[&fn, from](const auto &range) {
loop_(range, std::forward<Fn>(fn));
});
}我收到了这个错误:
Concurrency.hpp:43:32: error:使用类模板'blocked_range‘需要模板参数blocked_range.h:45:7:注意:模板在这里声明
以前有人遇到过这个错误吗?如何解决这个问题?
发布于 2020-10-01 13:08:49
tbb::blocked_range是一个类模板,您试图通过在构造它时省略任何显式模板参数来使用类模板参数演绎 (CTAD)。
模板类blocked_range { public: //!值/**的类型称为const_iterator,因为需要将blocked_range视为STL容器的算法。*/ typedef值const_iterator;// . //!用给定的粒度构造半开区间[开始,结束]。blocked_range( Value begin_,Value end_,size_type grainsize_=1 ) // . // . };
但是,CTAD是一个C++17特性,所以如果使用早期语言版本进行编译,则需要为tbb::blocked_range类模板指定Value类型模板参数。从上面我们可以看到,Value类型被期望为迭代器类型,特别是在调用站点上传递给它的构造函数from和to的前两个参数的类型。因此,您可以按以下方式修改代码段:
// Calling TBB parallel-for by this template
template<class It, class Fn>
static void for_each(It from, It to, Fn &&fn, size_t granularity = 1)
{
tbb::parallel_for(tbb::blocked_range<It>{from, to, granularity},
[&fn, from](const auto &range) {
loop_(range, std::forward<Fn>(fn));
});
}从您在评论中提到的内容来看,这可能是一个可移植性问题,在此之后您可能会遇到更多的问题,并且可能需要考虑查看项目的编译标志,以便查看是否可以使用C++17进行编译。
发布于 2020-10-01 13:08:40
错误说是'blocked_range' requires template arguments。
一种解决方案是添加一些模板参数。我不知道他们应该是什么,但也许
tbb::blocked_range<I>{from, to, granularity}
^^^
template argument herehttps://stackoverflow.com/questions/64155962
复制相似问题