警告:用两个参数构造函数返回对象时,表达结果未使用

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

本文关键字:对象 返回 未使用 结果 构造函数 参数 两个 警告      更新时间:2023-10-16

i有一个类,代表用于DSP处理的复杂有价值样本的缓冲区。对于一些清洁的代码,此类具有以下静态成员函数:

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)); }
}

这是按预期工作的,但是clang会抛出以下

Warning: "expression result unused". 
...
Note:(67, 75) in instantiation of function template specialization 'SampleBufferComplex<float>::castToSampleType<double>' requested here
...

我看不到在这里未使用任何表达式结果,但是我想编写100%警告免费代码。我是面对一些奇怪的编译器错误还是在这里忽略了一些明显的东西?任何指针都赞赏!

在表达式

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

突出显示的铸造表达的结果未使用。可以简化返回语句(假设第一个转换没有副作用):

return static_cast<SampleType> (im);

但是,我怀疑这不是您所掌握的(您启用了警告的好东西,是吗?)。也许您确实打算使用真实的部分?在这种情况下,您可能应该写的:

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