473,657 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

member template specialization

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)
Feb 18 '06 #1
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
Feb 18 '06 #2
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)
Feb 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5770
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...
4
2578
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(); }
9
1617
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>(); }
3
2114
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?
5
6579
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>
1
2205
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;
4
3326
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 {
2
7690
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:
13
6579
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...
5
3774
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)
0
8823
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, 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...
0
8726
jinu1996
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...
1
8503
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,...
0
8603
tracyyun
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...
0
7320
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, 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...
0
4151
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...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.