Hi,
I am having trouble with templates. In a non-template class, I have
a template function. Now I want to be able to execute different code for
all pointer types. But I cannot get the syntax right. Here is small
snippet demonstrating the problem.
struct bar
{
template <typename T>
T foo ();
};
template <typename T>
T bar::foo ()
{
// specialization for 'real' types
T t;
return t;
}
template <typename U>
template <>
U* bar::foo <U*> ()
{
// specialization for all pointer types
U* u;
return u;
}
Thanks for your time!
--
jb
(reply address in rot13, unscramble first) 2 1825
"Jakob Bieling" <ar************ ****@rot13.com> wrote in message
news:dt******** *****@news.t-online.com Hi,
I am having trouble with templates. In a non-template class, I have a template function. Now I want to be able to execute different code for all pointer types. But I cannot get the syntax right. Here is small snippet demonstrating the problem.
struct bar { template <typename T> T foo (); };
template <typename T> T bar::foo () { // specialization for 'real' types T t; return t; }
template <typename U> template <> U* bar::foo <U*> () { // specialization for all pointer types U* u; return u; }
Thanks for your time! -- jb
(reply address in rot13, unscramble first)
The problem here is that you are not specializing the member, you are
partially specializing it. This is not allowed. You can only partially
specialize a whole class, e.g.,
// general case
template<typena me T>
struct bar
{
T foo ();
};
template <typename T>
T bar<T>::foo ()
{
T t;
return t;
}
// partial specialization for all pointer types
template <typename T>
struct bar<T*>
{
T* foo ();
};
template <typename T>
T* bar<T*>::foo ()
{
T* pt;
return pt;
}
--
John Carson
John Carson <jc************ ****@netspace.n et.au> wrote: "Jakob Bieling" <ar************ ****@rot13.com> wrote in message news:dt******** *****@news.t-online.com Hi,
I am having trouble with templates. In a non-template class, I have a template function. Now I want to be able to execute different code for all pointer types. But I cannot get the syntax right. Here is small snippet demonstrating the problem.
struct bar { template <typename T> T foo (); };
template <typename T> T bar::foo () { // specialization for 'real' types T t; return t; }
template <typename U> template <> U* bar::foo <U*> () { // specialization for all pointer types U* u; return u; }
The problem here is that you are not specializing the member, you are partially specializing it. This is not allowed. You can only partially specialize a whole class, e.g.,
Oh, thanks for the clarification! Luckily, I was pursuing the wrong
approach and I am not dependant on this solution :)
Thanks!
--
jb
(reply address in rot13, unscramble first) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jeff |
last post by:
/* --------------------------------------------------------------------------
Hello,
I was experimenting with class templates and specializing member
functions and came across a simple problem which I can't figure out.
I have a simple class C with 2 template member objects, and a function
print() that prints the value of these objects. I defined a
specialization of print() for C<string, char> which is called
correctly. Then I wanted...
|
by: SainTiss |
last post by:
Hi,
From what I've read in several places, it seems that explicit specialization
of member functions of class templates is allowed, but partial
specialization isn't:
template<class T, class R> class A {
void foo();
}
|
by: Patrick Kutch |
last post by:
Please take a look at the following code snippit:
-------------------------------------------------------
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
SpecializedFunct_<TraitClass>();
}
|
by: Dave |
last post by:
Hello all,
I am trying to create a full specialization of a member function template of
a class template. I get the following errors:
Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
Line 29: 'bar' : unable to match function definition to an existing
declaration
What am I doing wrong?
|
by: Levent |
last post by:
Hi,
Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1):
#include <iostream>
template<class T, unsigned N>
struct Foo {
void func();
};
template<class T, unsigned N>
| |
by: Kai-Uwe Bux |
last post by:
Hi folks,
I would like to know which clause of the standard rules out this:
template < typename eval >
struct recursive_template {
typedef typename eval::enum_type Enum;
|
by: Joseph Turian |
last post by:
Hi,
What is the correct syntax to get the bar<T>::f<int, unsigned>()
function to compile in the following fragment?
Thanks,
Joseph
class foo {
|
by: Barry |
last post by:
The following code compiles with VC8
but fails to compiles with Comeau online,
I locate the standard here:
An explicit specialization of any of the following:
|
by: mike b |
last post by:
Hello everyone, thanks in advance for your help. I'm new to C++
templates and have run into some issues using member function
templates. I have a shared library containing templates that I'm
trying to use from an executable, compile using gcc 4.1.2. Everything
works fine until I try specializing one of the static member function
templates in a non-template class. I have a feeling I'm messing up
something obvious so before I post a...
|
by: chgans |
last post by:
Hi all,
I'm having difficulties with some template static member, especially
when this member is a template instance, for example:
----
template<typename T>
class BaseT
{
public:
static void UseMap (const std::string &key, int value)
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |