vendredi 22 septembre 2017

C++ use lambda function as template function specialization

Is it possible to use a lambda function to create an alias to a template class function? Something like this:

#include <iostream>
using namespace std;

int calcDouble(int a) { return a * 2; }
int calcMultiply_10(int a) { return a * 10; }

struct foo
{
    template<void (*func)(int)>
    void generic(int value)
    {
        cout << func(value) << endl;
    }

    static auto double_10 = [this] { generic<calcDouble>(10); };
    static auto double_20 = [this] { generic<calcDouble>(20); };
    static auto multiply_10_20 = [this] { generic<calcMultiply_10>(20); };
}


int main() {
    foo f;

    cout << "double_10: " <<f.double_10() << endl;
    cout << "double_20: " <<f.double_20() << endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire