473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

template specialization - recursive control path issue.


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
9 1645
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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
7
by: wogston | last post by:
A) template <typename scalar, int size> struct basevector { enum { size = size }; scalar v; }; B)
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
7
by: er | last post by:
hi, could someone please help with this code? template<unsigned int N,unsigned int M> class A{ public: A(); };
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
3
by: nooneinparticular314159 | last post by:
I'm trying to do a template with specialization. I declare two templates, one of which looks like: template<int a, int b> struct MOO { enum{ VALUE = b == 0 ? a: MOO<some stuff>::VALUE };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.