Search code examples
c++templateslambdavisual-studio-2019template-specialization

Is possible to invoke a templated lambda with explicit specialization?


I'm trying to use a lambda which have two specializations, but it seems I'm doing something wrong. I tried to search here, but I couldn't find nothing, except this:

How to invoke a lambda template?

Which doesn't help too much in my case. Please, could you tell me how should I invoke lambdas with my specializations? I'm working with Visual Studio 2019 16.9.2 (I cannot update yet)

auto testLamb = []<typename T, int max = 2>(T data)
{
    if constexpr (max == 0)
    {
        return data;
    }
    else
    {
        return data + max;
    }
};

int f1 = testLamb<int, 4>(4); // Error C2062 !!
int f2 = testLamb(5); // OK!

Solution

  • It is not the class/variable which is template, but its operator:

    testLamb.operator()<int, 4>(4);