435,335 Members | 2,313 Online
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

# template specialization - recursive control path issue.

 P: n/a I'm following section 35 of the FAQ and am I'm trying to do some specialization. Trouble is I have a recursive problem here that I'm not sure how to solve. Ideas? The snippet: # include template void foo_test ( T const* ptr ){} template<> inline void foo_test ( const float* ptr ) { std::cout << " float " << std::endl; foo_test ( ptr ); } template<> inline void foo_test ( const double* ptr ) { std::cout << " double " << std::endl; foo_test ( ptr ); } int main ( void ) { double t ( 5 ); double *ptr_t = &t; foo_test(ptr_t); } Thanks in advance. Apr 10 '06 #1
9 Replies

 P: n/a ma740988 wrote: I'm following section 35 of the FAQ and am I'm trying to do some specialization. Trouble is I have a recursive problem here that I'm not sure how to solve. Ideas? Not before you tell us what you're trying to accomplish. The snippet: It would better to see a complete program. # include template void foo_test ( T const* ptr ){} template<> inline void foo_test ( const float* ptr ) { std::cout << " float " << std::endl; foo_test ( ptr ); So, you're calling the same function here? Or what? } template<> inline void foo_test ( const double* ptr ) { std::cout << " double " << std::endl; foo_test ( ptr ); } int main ( void ) { double t ( 5 ); double *ptr_t = &t; foo_test(ptr_t); } V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask Apr 10 '06 #2

 P: n/a Victor Bazarov wrote: Not before you tell us what you're trying to accomplish. Would like to be able to call foo_test for float and double. So within main I should be able to do: foo_test(ptr_t); foo_test (ptr_t); Apr 10 '06 #3

 P: n/a ma740988 wrote: Victor Bazarov wrote: Not before you tell us what you're trying to accomplish. Would like to be able to call foo_test for float and double. So within main I should be able to do: foo_test(ptr_t); foo_test (ptr_t); I asked you which 'foo_test' you were trying to call _inside_ each of those specialisations. Why do I have to repeat myself? V -- Please remove capital As from my address when replying by mail Apr 11 '06 #4

 P: n/a I asked you which 'foo_test' you were trying to call _inside_ each of those specialisations. Why do I have to repeat myself? That's probably because I dont understand what you're asking me. foo_test(ptr_t); should call template<> inline void foo_test ( const double* ptr ) which calls template void foo_test ( T const* ptr ){} What am I missing? Apr 11 '06 #5

 P: n/a ma740988 wrote: I asked you which 'foo_test' you were trying to call _inside_ each of those specialisations. Why do I have to repeat myself? That's probably because I dont understand what you're asking me. foo_test(ptr_t); should call template<> inline void foo_test ( const double* ptr ) which calls template void foo_test ( T const* ptr ){} What am I missing? When foo_test( ptr_t ) calls, as you say, template void foo_test ( T const* ptr ){} it does not really do that: what it calls is a particular instantiation of that template which is deduced by looking at the types of the actual parameters passed in the call. That type, as it happens, will be: double const *, i.e., foo_test( ptr_t ) calls foo_test( ptr_t ) Very likely, that is not what you want. Best Kai-Uwe Bux Apr 11 '06 #6