By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,313 Online
Bytes IT Community
+ Ask a Question
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 <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.

Apr 10 '06 #1
Share this Question
Share on Google+
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 <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
--
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<double>(ptr_t);
foo_test<float> (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<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
--
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<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?

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<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
Apr 11 '06 #6

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.


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
--
Please remove capital As from my address when replying by mail
Apr 11 '06 #7

P: n/a

Victor Bazarov wrote:
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.


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

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.

Apr 11 '06 #8

P: n/a
ma740988 wrote:

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);
}

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 11 '06 #9

P: n/a

Pete Becker wrote:

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);

Apr 11 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.