2012-11-29 3 views
0

에도 명시 적으로 함수 포인터의 매개 변수가 const를 것을 언급함으로써, 이러한 유형의 기능을 변환 할 수하지 않는 것 :const 매개 변수가있는 함수를 함수 포인터로 변환 할 수 없습니까?

#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); 
} 

사람이 그 변환을 수행하는 방법을 설명 할 수 있습니까?

+0

는, 현재 매개 변수에 imagetype * const''로 확장합니다. 'decltype'을 통해 쉽게 이해할 수 없습니다. – Xeo

+0

어쨌든 C++ 11을 사용한다면 왜'auto'가 아닌가? – leftaroundabout

+0

@leftaroundabout - 내가 사용하고자하는 오버로드를 지정해야합니다 (http://stackoverflow.com/questions/13632507/how-to-get-a-function-pointer-to-the-overloaded-function-that 참조 자세한 것은 compiler-choos를 참조하십시오). –

답변

1

const 포인터에 적용됩니다. 예상대로 FillImage() 작품의 image

const ImageType* image = new ImageType; 

의 첫 번째 버전을 변경하는 경우

그래서, const decltype(image)ImageType* const하지const ImageType*

에 해당합니다.

const ImageType* 당신이

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

그래서 'const ImageType *'을 어떻게 얻을 수 있습니까? –

+0

@DavidDoria :'CONST decltype (* 이미지) * const'을보십시오. – GManNickG

+1

는 @GManNickG이 작동하지 않는, decltype'때문에 (* 이미지)''에 imagetype 및'유형이고 당신은에 imagetype & const''를 정의 할 수 없습니다. –

0

FillImagestd::remove_pointer 템플릿이 아닌 기능입니다 사용할 수 있습니다 얻으려면. FillImage<float>의 변형을 시도해보십시오. 당신은`const`을 놓치고

관련 문제