Specialization by the function type
-
Please help to understand the following
#include <iostream> template<class F> struct function_caller; template<class R, class P1, class P2> struct function_caller<R (P1, P2)> { }; int main() { typedef function_caller<void (int, char)> my_caller; std::cout << typeid(my_caller).name() << std::endl; return 0; }The program output:
struct function_caller<void __cdecl(int,char)>Where I can read about specialization by the function types (in English) ?
-
Hi,
I hope your still waiting for an answer.
First of all the Output of
typeid().name()is compiler specific, if you didn't know that. For example my compile (gcc 4.1.1) gives me
15function_callerIFvicEEas output.
I don't know any resources for template specialization specifically for function types.
What you do is a partial template specialization to get your template code to work with function types as template parameters while extracting Informations about return and argument types of the function. And that's a little bit tricky as you can see in your code.
If you have more questions or if I need to describe it more detailed just ask for it

And for your english resources (not everything might seem useful at first):
http://www.parashift.com/c++-faq-lite/index.html
http://www.decadentplace.org.uk/womble/c++/template-faq.html
http://www.tutok.sk/fastgl/callback.html
-
typeid().name() is compiler specific, if you didn't know that. For example my compile (gcc 4.1.1) gives me ...
My .name() is better than yours :p
If you have more questions or if I need to describe it more detailed just ask for it

Yes! I have questions:
- Why line 3 in the code is necessary?
- Can I achieve the same result (passing a function type into template) without using the specialization technique?
-
1. Because the function is defined AFTER the function that uses it. So otherwise the compiler wouldn't even know that it is there. It is a bit lazy and stops looking for a function when it is about to be used instead of parsing the rest of the file. This "prototyping" shows the compiler that there will be a function like that later on and that he shouldn't worry about it.

-
Fellhuhn I know what is prototyping. But im my code there is no prototyping. It is a "template specialization".
IMHO.
-
It is a declaration of the template itself. Because if there is no template "function_caller", there can't be any specialization of "function_caller". And to let the compiler know that there is (at least let him assume there is), you declare it before specializing it.
-
LordJaxom. OK, thank you. What about my question #2 ?
-
#include <iostream> template<class F> struct function_caller; template<class R, class P1, class P2> struct function_caller<R (P1, P2)> { }; int main() { typedef function_caller<void (int, char)> my_caller; std::cout << typeid(my_caller).name() << std::endl; return 0; }SAn schrieb:
Can I achieve the same result (passing a function type into template) without using the specialization technique?
yes, you can:
#include <iostream> template<class F> struct function_caller { }; int main() { typedef function_caller<void (int, char)> my_caller; std::cout << typeid(my_caller).name() << std::endl; return 0; }the first declaration of function_caller only tells the compiler that it has one template parameter. and
void (int,char)is not a compound, but one distinct type (which is used as a template argument in this example)
-
queer_boy schrieb:
...the first declaration of function_caller only tells the compiler that it has one template parameter. and
void (int,char)is not a compound, but one distinct type (which is used as a template argument in this example)Hmm...

Thank you.