非常量引用返回函数在常量值返回函数上用作 r 值
Non-const reference-returning function getting used as r-value over const value-returning function
我有两个版本的函数。一个按值返回,旨在用作 r 值。另一个通过引用返回,旨在用作 l 值(写入audio_data向量(。
float AudioData::sample(unsigned frame, unsigned channel) const
{
return audio_data[frame * channel_count + channel];
}
float& AudioData::sample(unsigned frame, unsigned channel)
{
clearSplits(); // Clear out cached information that may be invalidated by this call
return audio_data[frame * channel_count + channel];
}
在调试时,我注意到引用返回覆盖被用作 r 值:
const float *AudioData::split(unsigned channel)
{
if (splits[channel] != nullptr) return splits[channel];
if (channel_count == 1) return data();
float *split = new float[frame_count];
for (unsigned i = 0; i < frame_count; ++i)
{
split[i] = sample(i, channel); // Calls the reference-returning override
}
splits[channel] = split;
return split;
}
void AudioData::clearSplits() // This gets called while writing to the splits!
{
for (unsigned i = 0; i < splits.size(); ++i)
{
if (splits[i] != nullptr) delete[] splits[i];
splits[i] = nullptr;
}
}
为什么编译器在不修改返回引用时选择使用非 const l 值覆盖,我如何防止它这样做?
重载解析根本不关心函数的返回类型。它所关心的只是调用的参数类型。在您的情况下,由于您从split
(一个非 const 限定的函数(调用sample
,因此隐式对象参数 (this
( 不是 const 限定的。因此,非常量重载被调用,并返回一个引用。
一个简单的解决方案是提供另一个函数,另一个名称,也返回一个副本。
float AudioData::csample(unsigned frame, unsigned channel) const
{
return sample(fram, channel); // will always call the const overload
}
如果您反对这样做,那么您可以随时const_cast
调用正确的重载:
split[i] = const_cast<AudioData const *>(this)->sample(i, channel);
相关文章:
- 从函数返回const char*数组
- 检查函数返回类型是否与STL容器类型值相同
- 从 c 或 cpp 系列子函数返回到主函数
- 从函数返回任意简单类型的数据
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 从 C++ 中的函数返回数组地址问题
- VirtDisk.h QueryChangesVirtualDisk() 函数返回 RangeCount 为 0
- 为什么glGetSubroutineIndex为不同的函数返回相同的值?
- 在 c++ 中将函数返回类型指定为模板参数
- 从封装在对象中的函数 C++ 返回时为空的列表
- 为什么向量内部的指针在从函数返回时会发生变化?
- 函数返回的 rvalue 引用(表达式)是 xvalue - 但没有标识?
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 寿命延长从函数返回引用
- 程序不向函数返回值
- 字符串函数返回奇怪的值
- 类的大问题,以及从空函数c++返回
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- C++二叉搜索树模板从函数返回节点
- NVCC 错误:string_view.h:constexpr 函数返回是非常量