Take the 2-minute tour ×

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;

Can anyone explain how to make it do that conversion?

share|improve this question
You're missing a const, currently the parameters expand to ImageType* const. You can't easily get that through decltype. –  Xeo Nov 29 '12 at 23:57
If you're using C++11 anyway, why not just auto? –  leftaroundabout Nov 30 '12 at 0:03
@leftaroundabout - I have to specify the overload that I want to use (see stackoverflow.com/questions/13632507/… for more details). –  David Doria Nov 30 '12 at 0:05
add comment

2 Answers

up vote 1 down vote accepted

The const applies to the pointer.

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

If you change image to

const ImageType* image = new ImageType;

the first version of FillImage() works as expected.

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

#include <type_traits>
void (*autoFunctionPointer)(const std::remove_pointer<decltype(image)>::type *) = FillImage;
share|improve this answer
so how would I get 'const ImageType*' then? –  David Doria Nov 30 '12 at 0:05
@DavidDoria: Try const decltype(*image)* const. –  GManNickG Nov 30 '12 at 0:14
@GManNickG This doesn't work, because decltype(*image) is of type ImageType& and you cannot define ImageType& const. –  Olaf Dietsche Nov 30 '12 at 0:17
I can confirm that this does not work. @OlafDietsche Any suggestions of things that might :) ? –  David Doria Nov 30 '12 at 0:26
@OlafDietsche: Oops. Then this should work: const std::remove_reference<decltype(*image)>::type* const. –  GManNickG Nov 30 '12 at 0:52
show 4 more comments

FillImage is a template, not a function. Try some variant of FillImage<float>.

share|improve this answer
add comment

Your Answer


