【问题标题】:Is restrict(...) not supported with lambda functions written in template classes?用模板类编写的 lambda 函数不支持 restrict(...) 吗?
【发布时间】: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;
}

【问题讨论】:

  • 在类方法中使用默认捕获([&amp;][=])是不好的做法。如果这些 lambda 将离开此上下文并在对象被销毁后被调用,您可能会得到未定义的行为。
  • @AlexUsachov 这是一个糟糕的启发式方法。按照这种逻辑,不应捕获任何具有引用或指针的内容。捕获this 是 lambda 语法的一个重要方面
  • @AlexUsachov 不正确。 [=] 专门复制变量。 [&amp;] 可能导致悬空引用。一起使用 std::shared_ptr[=] 确保类的成员仍然可以生存,直到 lambda 超出范围。

标签: c++ templates lambda c++-amp


【解决方案1】:

我最近遇到了同样的问题。此错误是由 C++ 编译器选项“一致性模式”(项目属性 > C/C++ > 语言)引起的,该选项似乎在最近的 VS 版本中设置为默认启用。将此选项设置为 no,您的代码应该可以编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多