template specialization - recursive control path issue.

M

ma740988

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 <iostream>

template <typename T>
void foo_test ( T const* ptr ){}

template<> inline void foo_test<float> ( const float* ptr )
{ std::cout << " float " << std::endl; foo_test ( ptr ); }
template<> inline void foo_test<double> ( const double* ptr )
{ std::cout << " double " << std::endl; foo_test ( ptr ); }

int main ( void )
{
double t ( 5 );
double *ptr_t = &t;

foo_test<double>(ptr_t);
}

Thanks in advance.
 
V

Victor Bazarov

ma740988 said:
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 <iostream>

template <typename T>
void foo_test ( T const* ptr ){}

template<> inline void foo_test<float> ( 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<double> ( const double* ptr )
{ std::cout << " double " << std::endl; foo_test ( ptr ); }

int main ( void )
{
double t ( 5 );
double *ptr_t = &t;

foo_test<double>(ptr_t);
}

V
 
M

ma740988

Victor said:
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<double>(ptr_t);
foo_test<float> (ptr_t);
 
V

Victor Bazarov

ma740988 said:
Would like to be able to call foo_test for float and double. So
within main I should be able to do:

foo_test<double>(ptr_t);
foo_test<float> (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
 
M

ma740988

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<double>(ptr_t);
should call
template<> inline void foo_test<double> ( const double* ptr )
which calls
template <typename T>
void foo_test ( T const* ptr ){}
What am I missing?
 
K

Kai-Uwe Bux

ma740988 said:
That's probably because I dont understand what you're asking me.

foo_test<double>(ptr_t);
should call
template<> inline void foo_test<double> ( const double* ptr )
which calls
template <typename T>
void foo_test ( T const* ptr ){}
What am I missing?

When

foo_test<double>( ptr_t )

calls, as you say,

template <typename T>
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<double>( ptr_t ) calls foo_test<double>( ptr_t )

Very likely, that is not what you want.


Best

Kai-Uwe Bux
 
V

Victor Bazarov

ma740988 said:
That's probably because I dont understand what you're asking me.

Did you read your original post? Did you read my reply to it?
foo_test<double>(ptr_t);
should call
template<> inline void foo_test<double> ( const double* ptr )
which calls
template <typename T>
void foo_test ( T const* ptr ){}
What am I missing?

Common sense, most likely.


.. template<class T> int foo(T t) { return 42; }
.. template<> int foo<double>(double d) { return foo(d); }
.. ^^^^^^^^^^^^^

Why in hell would the marked call be to the generic template, when 'd'
is already a 'double' and there is a specialisation for 'double'?

If you intended to call the non-specialised version of 'foo', then you
don't need to specialise the template. Just use overloaded functions:

template<class T> int foo(T t) { return 42; }
int foo(double d) { return foo<double>(d); }

and you will have no recursion.

V
 
M

ma740988

Victor said:
Did you read your original post? Did you read my reply to it?
Some of us aren't as well versed are you are. Sometimes, I get
entangled in the syntax.
Common sense, most likely.
These are times, when I think you could do me a favor and find somebody
else's post to answer.

[...]
If you intended to call the non-specialised version of 'foo', then you
don't need to specialise the template. Just use overloaded functions:
I realized that last night after digging deeply.
 
P

Pete Becker

ma740988 said:
Sometimes, I get
entangled in the syntax.

Many problems that show up when people write templates also show up
without the templates. When you're lost in template land, leave it. Try
something similar without templates:

void f(const double*ptr)
{
cout << "float\n";
f(ptr);
}
 
M

ma740988

Pete said:
Many problems that show up when people write templates also show up
without the templates. When you're lost in template land, leave it. Try
something similar without templates:
I agree with you. As soon as I caught on to the overload. It became
something really inane on my part. As Bazarov pointed out, I needed:

return foo<double>(d);

In which case it's the reverse of what I'm accustomed to:

return foo ( d );

So that's what I was doing.

Until it _literally_ hit me. I needed:

return foo<double>(d);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top