Nested templates explicit specialization

Discussion in 'C++' started by Andriy Shnyr, Dec 3, 2003.

  1. Andriy Shnyr

    Andriy Shnyr Guest

    Let us consider the following nested templates case:

    template<typename T>
    class Outer{

    public:
    template<typename U>
    class Inner{

    public:
    void f();
    };

    };

    Then the following specialization
    template<> template<typename U> void Outer<int>::Inner<U>::f(){};
    results in compile-time error (I've tried to compile it with gcc 3.2.2)

    main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
    main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
    main.cpp:XXX: template definition of non-template `void
    Outer<int>::Inner<U>::f()'

    Can anyone help me to figure out the problem?

    Thanks in advnace,
    Andriy Shnyr

    ---
    [ comp.std.c++ is moderated. To submit articles, try just posting with ]
    [ your news-reader. If that fails, use mailto: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
    Andriy Shnyr, Dec 3, 2003
    #1
    1. Advertising

  2. Andriy Shnyr

    Dan W. Guest

    On Wed, 3 Dec 2003 17:35:21 +0000 (UTC), (Andriy
    Shnyr) wrote:

    >Let us consider the following nested templates case:
    >
    >template<typename T>
    >class Outer{
    >
    >public:
    > template<typename U>
    > class Inner{
    >
    > public:
    > void f();
    > };
    >
    >};
    >
    >Then the following specialization
    >template<> template<typename U> void Outer<int>::Inner<U>::f(){};
    >results in compile-time error (I've tried to compile it with gcc 3.2.2)
    >
    >main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
    >main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
    >main.cpp:XXX: template definition of non-template `void
    > Outer<int>::Inner<U>::f()'
    >
    >Can anyone help me to figure out the problem?
    >
    >Thanks in advnace,
    >Andriy Shnyr
    >
    >---
    >[ comp.std.c++ is moderated. To submit articles, try just posting with ]
    >[ your news-reader. If that fails, use mailto: ]
    >[ --- Please see the FAQ before posting. --- ]
    >[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


    template < typename O >
    struct outer
    {
    template < typename I >
    struct inner
    {
    void f();
    };
    };

    template <>
    struct outer<int>
    {
    template < typename I >
    struct Inner
    {
    void f();
    };
    };

    Cheers!
    Dan W., Dec 3, 2003
    #2
    1. Advertising

  3. "Andriy Shnyr" <> wrote...
    > Let us consider the following nested templates case:
    >
    > template<typename T>
    > class Outer{
    >
    > public:
    > template<typename U>
    > class Inner{
    >
    > public:
    > void f();
    > };
    >
    > };
    >
    > Then the following specialization
    > template<> template<typename U> void Outer<int>::Inner<U>::f(){};
    > results in compile-time error (I've tried to compile it with gcc 3.2.2)
    >
    > main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
    > main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
    > main.cpp:XXX: template definition of non-template `void
    > Outer<int>::Inner<U>::f()'
    >
    > Can anyone help me to figure out the problem?


    If you need to specialise the outer class but keep the nested template
    a template, you need to first declare the existence of 'Outer<int>::Inner'
    template manually:

    template<> class Outer<int> {
    template<class U> class Inner {
    public:
    void f();
    };
    };

    Otherwise you're declaring a member function of a class that doesn't
    exist.

    That's my take on it, anyway.

    Victor
    Victor Bazarov, Dec 3, 2003
    #3
  4. Andriy Shnyr

    Larry Evans Guest

    On 12/03/2003 11:35 AM, Andriy Shnyr wrote:
    [snip]
    > main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
    > main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
    > main.cpp:XXX: template definition of non-template `void
    > Outer<int>::Inner<U>::f()'
    >
    > Can anyone help me to figure out the problem?

    IIRC, adding ::template Inner<U> solved the compile problem for me;
    however, I found a problem with the produced code not selecting the
    right specialization, as reported here:

    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13088

    ---
    [ comp.std.c++ is moderated. To submit articles, try just posting with ]
    [ your news-reader. If that fails, use mailto: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
    Larry Evans, Dec 4, 2003
    #4
    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. Patrick Kowalzick
    Replies:
    0
    Views:
    796
    Patrick Kowalzick
    Oct 29, 2004
  2. Andy
    Replies:
    5
    Views:
    490
    Shezan Baig
    Jan 30, 2005
  3. Vyacheslav Lanovets
    Replies:
    6
    Views:
    3,547
    Marc Mutz
    Aug 24, 2005
  4. Replies:
    1
    Views:
    552
    Salt_Peter
    Dec 25, 2006
  5. ld
    Replies:
    8
    Views:
    301
Loading...

Share This Page