0

I'm writing a templated audio class (with typename T) for audio manipulation with binary data which is either int16_tor int8_t. Lambda functions are very new to me, so I don't know what is wrong with this function used to calculate the root mean square (RMS). Here is the code:

T calculate_RMS() {
        return [&] () {
            std::vector<T> squares;

            for(int i = 0; i < this->data_vector.size(); ++i) {
                squares.push_back(std::pow(this->data_vector[i], 2));
            }
            return std::sqrt(std::accumulate(squares.begin(), squares.end(), 0) / squares.size());
        };
}

The error being thrown is:

audio.h: In instantiation of ‘T YNGMAT005::Audio<T>::calculate_RMS() [with T 
= short int]’:
audiodriver.cpp:119:66:   required from here
audio.h:178:5: error: cannot convert ‘YNGMAT005::Audio<T>::calculate_RMS() 
[with T = short int]::__lambda0’ to ‘short int’ in return
 };
 ^
audio.h: In instantiation of ‘T YNGMAT005::Audio<T>::calculate_RMS() [with T = signed char]’:
audiodriver.cpp:122:65:   required from here
audio.h:178:5: error: cannot convert ‘YNGMAT005::Audio<T>::calculate_RMS() 
[with T = signed char]::__lambda0’ to ‘signed char’ in return
make: *** [audiodriver.o] Error 1

I was testing this function using int8_tso I think that is why it says T is a short int.

Thanks

3
  • You're returning the lambda rather than the result of invoking the lambda. You need to append (). Commented Jun 2, 2017 at 11:27
  • What is the purpose of this lambda? Do you mean to return it to the call site or do you want to invoke it? If you want to invoke it and return a value then there is no need for a lambda here. Just remove it and the function will "just work". Commented Jun 2, 2017 at 11:31
  • In your code you are writing a function that returns a T value, but using a lambda that calculates it. Why? Why the need to use lambda? You can just write a standard template function e use that to do the work. Commented Jun 2, 2017 at 11:31

1 Answer 1

3

The fixed width integers are just typedefs, they're not a type in themselves. In your case, short int is exactly 1 byte wide.

The error is pretty clear:

error: cannot convert ‘YNGMAT005::Audio<T>::calculate_RMS() 
[with T = short int]::__lambda0’ to ‘short int’ in return

It can't convert the lambda to a short int. Remember that a lambda is just a function object, if you want to get the result of it, you need to call it:

    return [&] () {
        std::vector<T> squares;

        for(int i = 0; i < this->data_vector.size(); ++i) {
            squares.push_back(std::pow(this->data_vector[i], 2));
        }
        return std::sqrt(std::accumulate(squares.begin(), squares.end(), 0) / squares.size());
    }();
    ^^^^
  call the lambda

But why are you using a lambda for this? It doesn't really make sense, because you can just take the body of the lambda and put that as the body of the function... The only case where this technique (calling a lambda immediately) is when you need to initialize some constant from a complicated expression:

constexpr auto value = []() {
    std::array<int, 10> result;
    for (auto i = 0u; i < 10; ++i)
        result[i] = i * 5;
    return result;
}();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the comment! The only reason I'm doing this is to demonstrate that I can understand lambda functions to university lecturers, else I would have done it the easy way. :D
@MattYoung Using a lambda where it is not appropriate demonstrates to your lecturer that you have failed to understand lambdas, I'm afraid.
@Walter There is nothing to understand. All it is is an inline function. There is no practical use of one in this context since it is a very simple calculation (the lambda is never used as a filter/comparator at all), but we are forced to use one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.