473,386 Members | 2,042 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,386 software developers and data experts.

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 1481
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
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? ...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
5
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
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...
9
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...
6
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...
7
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,...
2
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.