c++ - 带有 const 重载和模板的模棱两可的调用
问题描述
在我的图像类中,我使用以下 const 重载模板函数提供对特定行的直接内存访问
template <typename T>
inline T* Image::getRow(unsigned int y)
{
...
}
template <typename T>
inline const T* Image::getRow(unsigned int y) const
{
...
}
但是,如果我指定另一个实际使用这些模板的函数:
template <typename T>
void Image::compute(...)
{
...
T* pImageRow = getRow<T>(y); //error C2668
...
}
我收到以下错误error C2668: 'Image::getRow': ambiguous call to overloaded function
。
有没有办法告诉编译器它实际上应该使用非常量变体Image::getRow
?
最小的例子
研究一个可重现的示例,我发现问题似乎更深一些(实际上,如果我getRow
从 lambda 函数调用parallelFor
):
最小的例子在这里:
inline void parallelFor(unsigned int endIndex, const std::function<void(unsigned int i)>& computeFunction, unsigned int startIdx = 0)
{
#pragma omp parallel for
for(int i = startIdx; i < (int)endIndex; i++)
{
computeFunction((unsigned int)i);
}
}
class Image
{
public:
template <typename T>
T* getRow(unsigned int y);
template <typename T>
const T* getRow(unsigned int y) const;
template <typename T>
void compute(const std::function<void(unsigned int x, unsigned int y, T& data)>& computeFunction);
};
template <typename T>
inline T* Image::getRow(unsigned int y)
{
return (T*)nullptr;
}
template <typename T>
inline const T* Image::getRow(unsigned int y) const
{
return (const T*)nullptr;
}
template <typename T>
void Image::compute(const std::function<void(unsigned int x, unsigned int y, T& data)>& computeFunction)
{
unsigned int rows = 10;
unsigned int cols = 10;
parallelFor(rows,
[&](unsigned int y)
{
T* pImageRow = getRow<T>(y);
for(unsigned int x = 0; x < cols; x++)
{
T& curVal = pImageRow[x];
computeFunction(x, y, curVal);
}
});
}
int main(int argc, char** argv)
{
Image a;
a.compute<unsigned char>([](unsigned int x, unsigned int y, unsigned char& color) { color = 0; });
return 0;
}
解决方案
添加const
到void Image::compute(...)
void Image::compute(...) const
如果这是不可能的,但 t 可以指向一个常量,那么做
template <typename T>
void Image::compute(...)
{
...
const T* pImageRow = static_cast<const Image*>(this)->getRow<T>(y);
...
}
如果两者都不可能,则向其中一个重载参数添加第二个虚拟参数。
推荐阅读
- javascript - 如何将文本重新格式化为不同的模式?
- docker - 如何在 docker compose 文件上设置 MySQL 和 phpmyadmin
- javascript - JS 不会将导入和声明的变量读入 console.log
- javascript - 尝试读取从反应 js 中的 API 返回的响应时获取未定义
- google-compute-engine - 谷歌云控制台 - 找不到 phpmyadmin 404
- selection - bootstrap-vue 选择带有过滤器选项的组件?
- android - 为什么 API Level 29 WiFi 连接需要密码?
- php - PHP Traits 通用和单个(单独)属性范围
- c# - 如何将滚动条与 RichTextBox 中的行匹配
- swift - 在 TableViewCell 中使用协议发现 Nil