Search code examples
c++templateslambdac++-amp

Is restrict(...) not supported with lambda functions written in template classes?


Recently I have been working with the amp (C++ Accelerated Massive Parallelism). Using this framework requires a lot of lambda expressions with restrict(amp). However, when I was trying to write these in a template class, the compiler throws an error message of Error C2760 syntax error: unexpected token 'identifier', expected '{'. However, it works perfectly without restricted(amp) or outside the template class. Here is the code that can reproduce such problem:

//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;
}

Solution

  • I ran into the same issue recently. This error is caused by the C++ compiler option "Conformance mode" (project properties > C/C++ > Language) which seems to be set to default enabled in the recent VS versions. Set this option to no and your code should compile.

    enter image description here