简体   繁体   中英

Warning: expression result unused when returning an object with a two argument constructor

I have a class representing a buffer of complex valued samples for DSP processing. For some clean looking code this class has the following static member function:

template <typename SampleType>
class SampleBufferComplex
{
public:

    ...

    /** Helper to create one Sample of the buffers SampleType in templated code */
    template <typename OriginalType>
    static std::complex<SampleType> castToSampleType (OriginalType re, OriginalType im) {return (static_cast<SampleType> (re), static_cast<SampleType> (im)); }

}

This works as expected, however clang throws the following

Warning: "expression result unused". 

...

Note:(67, 75) in instantiation of function template specialization 'SampleBufferComplex<float>::castToSampleType<double>' requested here

...

I cannot see where any expression result is unused here, however I want to write 100% warning free code. Am I facing some weird compiler bug or am I overlooking something totally obvious here? Any pointers appreciated!

In the expression

return (static_cast<SampleType> (re), static_cast<SampleType> (im));
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The result of the highlighted cast expression is unused. The return statement can be simplified into (assuming the first conversion has no side-effects):

return static_cast<SampleType> (im);

I suspect however, that this is not what you inteded (a good thing you have warnings enabled, eh?). Perhaps you did intend to use real part as well? In that case, you probably should have written instead:

return {static_cast<SampleType> (re), static_cast<SampleType> (im)};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM