template <typename T, std::size_t N>
static T sum(const std::array<T, N>& a)
{
T result;
// type of result (T) is not determined when pre-process?
#pragma omp parallel for reduction(+: result)
for(int i = 0; i < static_cast<int>(N); i++)
{
result += a[i];
}
return result;
}
我可以使用MSVC和gcc编译和运行上述代码。是的,这很棒!但我的问题在于代码注释中:“因为在预处理'#pragma'时结果(T)的类型未确定,编译器如何验证结果的类型是否适用于OpenMP reduction?” 我确定当T = double时,它是可以的,而当T = std :: string时,它是不可行的,但是预处理器如何知道T的类型?我记得很久以前我无法使用某些小型C ++编译器编译上述代码。让我问一下,在C ++ / OpenMP规范的背景下,哪种行为(可编译或不可编译)是正确的。
#pragma omp
是用于编译器支持OpenMP的指令,而不是预处理器。 - yohjp