473,769 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specializing on a templated type

Hello,

Is there any way of specializing a templated function on a type which
is itself templated? Here's the simplest example of the problem I can
think of. Say I have written an implementation of the identity function
as follows,

template <class T> T identity(T x) { return x; }

and for some reason I want to specialize this function for the
following templated type, so that instead of returning the entire
structure, the identity function only returns the single value within
the structure.

template <class T>
struct Foo {
T foo;
};

Is there any way I can do something like the following?

template <>
template <class FooT> // Parameter for Foo; not legal C++ code.
Foo<T> identity(T x) { return x.foo; }

Any help would be appreciated.

Thanks,
Alex

Sep 3 '05 #1
2 1504
Alex Drummond wrote:
Is there any way of specializing a templated function on a type which
is itself templated? Here's the simplest example of the problem I can
think of. Say I have written an implementation of the identity
function as follows,

template <class T> T identity(T x) { return x; }

and for some reason I want to specialize this function for the
following templated type, so that instead of returning the entire
structure, the identity function only returns the single value within
the structure.

template <class T>
struct Foo {
T foo;
};

Is there any way I can do something like the following?

template <>
template <class FooT> // Parameter for Foo; not legal C++ code.
Foo<T> identity(T x) { return x.foo; }

Any help would be appreciated.


Just define an overloaded function:
--------------------------------------------------------------------
template <class T> T identity(T x) { return x; }

template <class T>
struct Foo {
T foo;
};

// the following is NOT a specialisation, it's an overloaded function
template<class T> T identity(Foo<T> x) { return x.foo; }

int main() {
Foo<int> x = { 42 };
identity(x);
}

--------------------------------------------------------------------

V
Sep 3 '05 #2
Great, thanks :)

Alex

Sep 3 '05 #3

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

Similar topics

3
6576
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? Here's what I'm generally trying to achieve: I'm building (trying to anyway) a serialization library. Here's my design:
2
5778
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...
5
1696
by: Marijn | last post by:
I recently told g++ (3.2) something to the effect of: template <class T> struct test{ typedef int type; }; template <class T> void f(){ test<T>::type i;
2
2013
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions that print out a type's name to an istream:
9
2318
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
6
1315
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated columns work find as long as I pass in a dataset but if I pass in a collection of objects then they won't work unless I can cast the correct object and then fill in the column with the object. My current code looks like this: public void...
7
1729
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum, short Trank, bool Tcont> TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont& tten );
2
2936
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
2
2290
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include <stack> template <class T> class A { public:
0
9583
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, 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...
0
10039
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
9990
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
9860
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
8869
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...
1
7406
isladogs
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...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
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
3560
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.