Can't convert a function with const parameter to a function pointer?

Even if you explicitly indicate that the parameter in the function pointer is const, it does not 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 someone explain how to get this to do this conversion?

0
source share
2 answers

The pointer refers to const .

So const decltype(image) equivalent to ImageType* const , not const ImageType*

If you change image to

 const ImageType* image = new ImageType; 

The first version of FillImage() works as expected.

To get const ImageType* , you can use std :: remove_pointer

 #include <type_traits> ... void (*autoFunctionPointer)(const std::remove_pointer<decltype(image)>::type *) = FillImage; 
+1
source

FillImage is a template, not a function. Try the FillImage<float> option.

0
source

All Articles