Specializing on a templated type

Discussion in 'C++' started by Alex Drummond, Sep 3, 2005.

  1. 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
    Alex Drummond, Sep 3, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Sep 3, 2005
    #2
    1. Advertising

  3. Great, thanks :)

    Alex
    Alex Drummond, Sep 3, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    378
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    436
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,210
  4. Amadeus W. M.
    Replies:
    2
    Views:
    380
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    291
Loading...

Share This Page