Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Specializing on a templated type

Reply
Thread Tools

Specializing on a templated type

 
 
Alex Drummond
Guest
Posts: n/a
 
      09-03-2005
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-03-2005
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


 
Reply With Quote
 
 
 
 
Alex Drummond
Guest
Posts: n/a
 
      09-03-2005
Great, thanks

Alex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling templated member of templated object david@sunlightd.com C++ 1 06-22-2007 05:13 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments