无法将具有const参数的函数转换为函数指针? [英] Can't convert a function with a const parameter to a function pointer?
本文介绍了无法将具有const参数的函数转换为函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使明确提到函数指针中的参数是const,它似乎不能将函数转换为此类型:
Even by explicitly mentioning that the parameter in the function pointer is const, it doesn't seem to be able to convert the function to this type:
#include <iostream>
template <typename T>
class Image{};
template <typename TPixel>
static void
FillImage(const Image<TPixel>* const image){}
//FillImage(Image<TPixel>* const image){} // Replacing the above line with this one compiles fine
int main()
{
typedef Image<float> ImageType;
ImageType* image = new ImageType;
void (*autoFunctionPointer)(const decltype(image)) = FillImage;
autoFunctionPointer(image);
}
任何人都可以解释如何进行该转换?
Can anyone explain how to make it do that conversion?
推荐答案
const
适用于该指针。
所以, const decltype(image)
相当于 ImageType * const
/ em> const ImageType *
So, const decltype(image)
is equivalent to ImageType* const
and not const ImageType*
如果更改 image
到
const ImageType* image = new ImageType;
FillImage()的第一个版本
要获得 const ImageType *
,您可以使用 std :: remove_pointer
#include <type_traits>
...
void (*autoFunctionPointer)(const std::remove_pointer<decltype(image)>::type *) = FillImage;
这篇关于无法将具有const参数的函数转换为函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文