I wish to have a template method, which takes in data and processes it with a lambda function, whatever way the method itself wants to do that. However, I want the lambda function to get inlined so that the compiled assembly output won't end up having a "call" assembly instruction. Is this possible?
If it's not possible with lambdas, is there some other way to do that? Somehow using templates to pass a function as a template type or something?
I'm using C++17.
Below is an example of what I'm trying to achieve:
template <typename T> static inline void Process(const T* p_source1, const T* p_source2, T* p_destination, const int count, std::function<T (T, T)> processor) { for (int i = 0; i < count; i++) p_destination[i] = processor(p_source1[i], p_source2[i]); } void Process_Add(const uint8_t* p_source1, const uint8_t* p_source2, uint8_t* p_destination, const int count) { // How to make something like this lambda inline? auto lambda = [] (uint8_t a, uint8_t b) { return a + b; }; Process<uint8_t>(p_source1, p_source2, p_destination, count, lambda); }
typename U,U &&processor. This plus enabing optimization should do it.typename U), and replacestd::function<T (T, T)> processorwithU &&processor.