Partially specialize a template with another template class

Discussion in 'C++' started by Old Wolf, Apr 7, 2005.

  1. Old Wolf

    Old Wolf Guest

    I have a template class like this:

    template<typename T, int N>
    struct Foo
    {
    ...........
    };

    which I have successfully specialized for some types, eg:

    template<int N>
    struct Foo<std::string, N>
    { ...... }

    Is it possible to specialize it so that T is a std::pair<A,B>
    where A and B are new template parameters?
     
    Old Wolf, Apr 7, 2005
    #1
    1. Advertising

  2. Old Wolf wrote:
    > I have a template class like this:
    >
    > template<typename T, int N>
    > struct Foo
    > {
    > ...........
    > };
    >
    > which I have successfully specialized for some types, eg:
    >
    > template<int N>
    > struct Foo<std::string, N>
    > { ...... }
    >
    > Is it possible to specialize it so that T is a std::pair<A,B>
    > where A and B are new template parameters?


    And where would they come from? If neither A nor B are among the
    original template's arguments, how is it "specializing"?

    V
     
    Victor Bazarov, Apr 8, 2005
    #2
    1. Advertising

  3. Old Wolf

    Old Wolf Guest

    Victor Bazarov wrote:
    > Old Wolf wrote:
    > > I have a template class like this:
    > >
    > > template<typename T, int N>
    > > struct Foo
    > > {
    > > ...........
    > > };
    > >
    > > which I have successfully specialized for some types, eg:
    > >
    > > template<int N>
    > > struct Foo<std::string, N>
    > > { ...... }
    > >
    > > Is it possible to specialize it so that T is a std::pair<A,B>
    > > where A and B are new template parameters?

    >
    > And where would they come from?


    That's what I was asking

    > If neither A nor B are among the original template's arguments,
    > how is it "specializing"?


    The original struct works for any type, the specialization
    I want works for a std::pair (which is a subset of all types).
     
    Old Wolf, Apr 8, 2005
    #3
  4. On 2005-04-07, Victor Bazarov <> wrote:
    > Old Wolf wrote:
    >> I have a template class like this:
    >>
    >> template<typename T, int N>
    >> struct Foo
    >> {
    >> ...........
    >> };
    >>
    >> which I have successfully specialized for some types, eg:
    >>
    >> template<int N>
    >> struct Foo<std::string, N>
    >> { ...... }
    >>
    >> Is it possible to specialize it so that T is a std::pair<A,B>
    >> where A and B are new template parameters?

    >
    > And where would they come from? If neither A nor B are among the
    > original template's arguments, how is it "specializing"?


    Maybe he means something like the following. It's specializing, because
    std::pair<A,B> is the first argument to the template.

    This compiles with gcc 3.3.5 with standards options turned up. But is it
    correct ?

    #include <utility>
    #include <string>
    template<typename T, int N> struct Foo { };
    template<int N> struct Foo<std::string, N> { };

    template <typename A, typename B>
    struct Foo< std::pair<A,B>, 3> { void f() {} };

    int main() { Foo<std::pair<int,double>, 3> x; x.f(); }

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, Apr 8, 2005
    #4
  5. Donovan Rebbechi wrote:
    > On 2005-04-07, Victor Bazarov <> wrote:
    >
    >>Old Wolf wrote:
    >>
    >>>I have a template class like this:
    >>>
    >>> template<typename T, int N>
    >>> struct Foo
    >>> {
    >>> ...........
    >>> };
    >>>
    >>>which I have successfully specialized for some types, eg:
    >>>
    >>> template<int N>
    >>> struct Foo<std::string, N>
    >>> { ...... }
    >>>
    >>>Is it possible to specialize it so that T is a std::pair<A,B>
    >>>where A and B are new template parameters?

    >>
    >>And where would they come from? If neither A nor B are among the
    >>original template's arguments, how is it "specializing"?

    >
    >
    > Maybe he means something like the following. It's specializing, because
    > std::pair<A,B> is the first argument to the template.
    >
    > This compiles with gcc 3.3.5 with standards options turned up. But is it
    > correct ?
    >
    > #include <utility>
    > #include <string>
    > template<typename T, int N> struct Foo { };
    > template<int N> struct Foo<std::string, N> { };
    >
    > template <typename A, typename B>
    > struct Foo< std::pair<A,B>, 3> { void f() {} };
    >
    > int main() { Foo<std::pair<int,double>, 3> x; x.f(); }


    That makes sense... I even tried

    template<class A, class B, int N> struct Foo<std::pair<A,B>,N> ...

    and it was OK. My misunderstanding was simple: I thought that you are
    only allowed to shorten the list of arguments or change them slightly
    (as in T to T*, for example). Here we actually add arguments or totally
    replace them, which is kind of weird, I guess I am just not used to them.

    V
     
    Victor Bazarov, Apr 8, 2005
    #5
    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.

Share This Page