dhelmer

I'm creating a new thread but I'd like to supply a templated function to beginthreadex.

My thread function prototype is:

template<typename T>
unsigned int _stdcall GetArrayThread(void* pvStruct)

However, when a supply: "GetArrayThread<IClass1>" as the 3rd parameter to _beginthreadex, the compiler gives the following error:

error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (void *)' to 'unsigned int (__stdcall *)(void *)'

Passing "&GetArrayThread<IClass1>" makes no difference.

All was fine when 'GetArrayThread' was not templated, and I simply passed "GetArrayThread" as the 3rd parameter to _beginthreadex

Does anyone know if templated functions can be supplied to _beginthreadex



Re: Visual C++ Language Passing template function to _beginthreadex

einaros

Which version of VC is this

Templated functions are functions like any other, once instantiated, so they work just fine with callbacks such as those accepted by beginthreadex. If you're using VC8, and you've got SP1 installed; that looks like a bug to me. I'd be amazed if no one has found this 'till now, though.





Re: Visual C++ Language Passing template function to _beginthreadex

alex.m

Hi!

I can't reproduce the bug on vc++ 2008 pre-release, could you add a snipet

A blind try: use "__stdcall" instead of "_stdcall"





Re: Visual C++ Language Passing template function to _beginthreadex

dhelmer

I really should have mentioned I was using C++ with VS 6.0.

Another interesting tidbit - I tried getting the compiler to explicitly generate a specific "instance" of the templated fn, by adding:

unsigned int _stdcall GetArrayThread<IClass1>(void*);

at file scope, prior to the method that calls _beginthreadex. That eliminated the compiler error, but then the linker generated:

unresolved external symbol "unsigned int __stdcall GetArrayThread(void *)" ( GetArrayThread@@YGIPAX@Z)

Based on the posted replies, I tried compiling and linking the original code with VS 2003. The problem is eliminated so I'm thinking it must be a bug the VS 6.0 version of C++.

Thanks.





Re: Visual C++ Language Passing template function to _beginthreadex

einaros

dhelmer wrote:

The problem is eliminated so I'm thinking it must be a bug the VS 6.0 version of C++.



This is one of many template / conformance issues with VC6, I'm afraid.