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. 9 1663
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
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);
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 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?
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
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>(dou ble 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
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.
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.
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); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 18.3.1, 'Iseq'). I want the version for containers that he
gives, but also to provide a specialization for construction from a
pair<It,It> (eg because that is returned by equal_range()).
|
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, here's the situation
(ignore missing namespaces, etc, since I'm not cut-pasting this..)
// a.h
template <class B>
|
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 template for your
type. However, this requires you to copy the whole template, and
change the method, which leads to code duplication...
If there's only 1 template parameter, one can specialize the method
|
by: wogston |
last post by:
A)
template <typename scalar, int size>
struct basevector
{
enum { size = size };
scalar v;
};
B)
|
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();
};
| |
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{
Contents cn;
list < BTree_node < Contents children;
........
|
by: er |
last post by:
hi,
could someone please help with this code?
template<unsigned int N,unsigned int M>
class A{
public:
A();
};
|
by: Ioannis Gyftos |
last post by:
Hello,
First the code :)
///////////////////////////////////////////////////////////////////////////////////
// in another header file
namespace LJC{
|
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
};
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |