Templates & error C2440
-
I need some help. Here it the whole code snippet: #include "stdafx.h" template int IndirectFunction(int * pData) { return 0; } //also tried //int __cdecl IndirectFunction(int * pData) //{...} typedef int (*ONEPARAMFUNCTION)(void* param); //also tried //typedef int (__cdecl *ONEPARAMFUNCTION)(void* param); int main() { // <<<<<<<<< //error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'ONEPARAMFUNCTION' ONEPARAMFUNCTION t = (ONEPARAMFUNCTION) &IndirectFunction; // >>>>>>>>> return 0; } How can I make it work ?? I tried googling a little - C2440 seems to be pretty "popular" but I didn't find anything that could help me. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2440.asp also didn't help :( Seems it happens becouse of the template, if I remove it compiles ok (using only - int IndirectFunction(int * pData) without the template<>). If I replace: int IndirectFunction(int * pData) with int IndirectFunction(void * pData) it compiles ok. If I use templates and int* as parameter I get the error. Help! -- modified at 14:22 Friday 21st October, 2005
-
I need some help. Here it the whole code snippet: #include "stdafx.h" template int IndirectFunction(int * pData) { return 0; } //also tried //int __cdecl IndirectFunction(int * pData) //{...} typedef int (*ONEPARAMFUNCTION)(void* param); //also tried //typedef int (__cdecl *ONEPARAMFUNCTION)(void* param); int main() { // <<<<<<<<< //error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'ONEPARAMFUNCTION' ONEPARAMFUNCTION t = (ONEPARAMFUNCTION) &IndirectFunction; // >>>>>>>>> return 0; } How can I make it work ?? I tried googling a little - C2440 seems to be pretty "popular" but I didn't find anything that could help me. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2440.asp also didn't help :( Seems it happens becouse of the template, if I remove it compiles ok (using only - int IndirectFunction(int * pData) without the template<>). If I replace: int IndirectFunction(int * pData) with int IndirectFunction(void * pData) it compiles ok. If I use templates and int* as parameter I get the error. Help! -- modified at 14:22 Friday 21st October, 2005
-
try: int (*ONEPARAMFUNCITON)(int*) = IndirectFunction<int>; then: int x(666); cout << ONEPARAMFUNCITON(&x) << endl; should work! -- modified at 15:11 Friday 21st October, 2005
-
And it works perfectly! I would never think of this on my own - it looks wierd, but it works!! Thank you so much! H.
This is normal when using templates. The compiler only creates functions that are needed in your program. Son you can't create a function pointer to such function until it exists. When you first write
funtion<int>(666);
the compiler creates such function. Later you can get a function pointer from it. It seems odd but this is the way it needs to be done, with some sort of dummy call to the function Hope this explains the wierd behaveior codito ergo sum -
This is normal when using templates. The compiler only creates functions that are needed in your program. Son you can't create a function pointer to such function until it exists. When you first write
funtion<int>(666);
the compiler creates such function. Later you can get a function pointer from it. It seems odd but this is the way it needs to be done, with some sort of dummy call to the function Hope this explains the wierd behaveior codito ergo sum| Quote: >When you first write funtion(666); the compiler creates such function. Later you can get a function pointer from it. But that isn't what is happening (atleast I don't see it) - I first take the pointer and then call the function. And it works if I take the pointer to a function that has the exact signature, that is the int* parameter. The problems are created when I try to get a pointer to function and cast it to a function that takes a void* in the same command/line. template int IndirectFunction(int * pData) { return 0; } typedef int (*ONEPARAMFUNCTION_PVOID)(void* param); typedef int (*ONEPARAMFUNCTION_PINT)(int* param); int main() { ONEPARAMFUNCTION_PINT pF0 = &IndirectFunction; // ok!! ONEPARAMFUNCTION_PINT pF = IndirectFunction; // ok!! ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! ONEPARAMFUNCTION_PVOID pF3 = (ONEPARAMFUNCTION_PVOID)pF; // ok!! ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! int a =5; pF(&a); pF2(&a); pF3(&a); pF(&a); return 0; } The wierd part is that this works: ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! But this won't work: ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! But isn't this practicaly the same? IndirectFunction should have the same signature as the ONEPARAMFUNCTION_PINT so the cast shouldn't be needed and the second example should be the same as the first one (I even tried forcing the same calling convention, but MSVC7 still thinks that ONEPARAMFUNCTION_PINT and IndirectFunction have different signatures and requires the cast to ONEPARAMFUNCTION_PINT first) ???
-
| Quote: >When you first write funtion(666); the compiler creates such function. Later you can get a function pointer from it. But that isn't what is happening (atleast I don't see it) - I first take the pointer and then call the function. And it works if I take the pointer to a function that has the exact signature, that is the int* parameter. The problems are created when I try to get a pointer to function and cast it to a function that takes a void* in the same command/line. template int IndirectFunction(int * pData) { return 0; } typedef int (*ONEPARAMFUNCTION_PVOID)(void* param); typedef int (*ONEPARAMFUNCTION_PINT)(int* param); int main() { ONEPARAMFUNCTION_PINT pF0 = &IndirectFunction; // ok!! ONEPARAMFUNCTION_PINT pF = IndirectFunction; // ok!! ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! ONEPARAMFUNCTION_PVOID pF3 = (ONEPARAMFUNCTION_PVOID)pF; // ok!! ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! int a =5; pF(&a); pF2(&a); pF3(&a); pF(&a); return 0; } The wierd part is that this works: ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! But this won't work: ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! But isn't this practicaly the same? IndirectFunction should have the same signature as the ONEPARAMFUNCTION_PINT so the cast shouldn't be needed and the second example should be the same as the first one (I even tried forcing the same calling convention, but MSVC7 still thinks that ONEPARAMFUNCTION_PINT and IndirectFunction have different signatures and requires the cast to ONEPARAMFUNCTION_PINT first) ???
I have taken your code and everything compiles without errors.:confused: But guess what :), I have used MSVS 2005 (MS VC8) so this is certainly a bug in the VC7 compiler. So your code is right but your compiler (I use the same at work :(() missed the pedals, happens to the best of us:laugh: codito ergo sum
-
I have taken your code and everything compiles without errors.:confused: But guess what :), I have used MSVS 2005 (MS VC8) so this is certainly a bug in the VC7 compiler. So your code is right but your compiler (I use the same at work :(() missed the pedals, happens to the best of us:laugh: codito ergo sum