making a lambda template...

Hi,

I wanted to create a template lambda so that I do not have to re-define this behaviour for each possible type.. like so:

///// want to avoid this:
auto loggerDeleterWidget = [] (Widget * p ) {
std::cout << p << std::endl;
delete p;
};
auto loggerDeleterDoc = [] (Doc * p ) {
std::cout << p << std::endl;
delete p;
};
//////////

However the following does not compile:

template<typename T>
auto loggerDeleter = [] (T * p ) {
std::cout << p << std::endl;
delete p;
};


Any suggestions as to how to go about this?

Regards,
Juan
Why lambda? Can't you just make it a regular function template?
Why don't you just use a template function?

1
2
3
4
5
6
template <typename T>
void logDelete(T* t)
{
    std::cout << t << '\n' ;
    delete t;
}


And use logDelete<Type> where you would use a lambda?

You can use polymorphic lambdas as of C++14
1
2
3
4
[] (auto* p ) {
    std::cout << p << '\n';
    delete p;
};
Ok, yes I know I can use a function object... but, I wanted to see how far i could go with lamas. The polymorphic lambda suggested by Cubby seems what I was looking for..

Thanks to both of you!!

Juan
Topic archived. No new replies allowed.