【发布时间】:2018-04-20 10:44:58
【问题描述】:
最近我一直在使用 amp(C++ Accelerated Massive Parallelism)。使用这个框架需要大量带有restrict(amp) 的lambda 表达式。但是,当我尝试在模板类中编写这些内容时,编译器会抛出 Error C2760 syntax error: unexpected token 'identifier', expected '{' 的错误消息。但是,它可以在没有 restricted(amp) 或模板类之外完美运行。这是可以重现此类问题的代码:
//matrix2.cpp
#pragma once
#include "stdafx.h"
namespace rin
{
template <int V0, int V1>
class Matrix2
{
public:
Matrix2() : raw_(V0 * V1), view_(concurrency::extent<2>(V0, V1), raw_)
{
concurrency::parallel_for_each(concurrency::extent<2>(V0, V1),
[=](concurrency::index<2> idx)
restrict(amp)
{
});
auto fun = [=]() restrict(cpu)
{
std::cout << "It does not compile in a template class." << std::endl;
};
fun();
auto fun1 = [=]()
{
std::cout << "It does compile in a template class without the restrict(...)." << std::endl;
};
fun1();
}
std::vector<double> raw_;
concurrency::array_view<double, 2> view_;
};
}
//main.cpp
#include "stdafx.h"
#include "matrix.h"
using namespace rin;
using namespace concurrency;
int main()
{
Matrix2<5, 5> mat;
auto fun = [=]() restrict(cpu)
{
std::cout << "But outside the template class it does work!" << std::endl;
};
fun();
system("pause");
return 0;
}
【问题讨论】:
-
在类方法中使用默认捕获(
[&]或[=])是不好的做法。如果这些 lambda 将离开此上下文并在对象被销毁后被调用,您可能会得到未定义的行为。 -
@AlexUsachov 这是一个糟糕的启发式方法。按照这种逻辑,不应捕获任何具有引用或指针的内容。捕获
this是 lambda 语法的一个重要方面 -
@AlexUsachov 不正确。
[=]专门复制变量。[&]可能导致悬空引用。一起使用std::shared_ptr和[=]确保类的成员仍然可以生存,直到 lambda 超出范围。
标签: c++ templates lambda c++-amp