Explicit specialization of member class template

Discussion in 'C++' started by Barry, Sep 30, 2007.

  1. Barry

    Barry Guest

    The following code compiles with VC8
    but fails to compiles with Comeau online,

    [Error
    "ComeauTest.c", line 7: error: explicit specialization is not allowed in the
    current scope
    template <>
    ^

    "ComeauTest.c", line 10: error: explicit specialization is not allowed
    in the
    current scope
    template <>
    ^
    End-Error]


    I locate the standard here:

    [STD
    14.7.3 Explicit specialization [temp.expl.spec]
    An explicit specialization of any of the following:
    ¡ª function template
    ¡ª class template
    ¡ª member function of a class template
    ¡ª static data member of a class template
    ¡ª member class of a class template
    ¡ª member class template of a class template
    ¡ª member function template of a class template
    can be declared by a declaration introduced by template<>

    End-STD]

    if I didn't get it wrong, we can have member class template explicit
    specialization if the outter class is not templated. Right?


    ///////////////////////////////////////////

    struct A
    {
    private:
    template <class T>
    struct HasConvertionTo;

    template <>
    struct HasConvertionTo<long> {};

    template <>
    struct HasConvertionTo<bool> {};

    public:
    template <class T>
    operator T () const
    {
    return Convert(HasConvertionTo<T>()); // CT error trigger
    }

    private:
    template <class T>
    T Convert(HasConvertionTo<T>) const // dispatcher
    {
    return Convert(T());
    }

    long Convert(long) const
    {
    return 10;
    }

    bool Convert(bool) const
    {
    return true;
    }
    };

    int main()
    {
    A a;

    long l = a;

    if (a) {
    }
    }
    --
    Thanks
    Barry
    Barry, Sep 30, 2007
    #1
    1. Advertising

  2. Barry

    Barry Guest

    Barry wrote:

    > [STD
    > 14.7.3 Explicit specialization [temp.expl.spec]
    > An explicit specialization of any of the following:
    > ¡ª function template
    > ¡ª class template
    > ¡ª member function of a class template
    > ¡ª static data member of a class template
    > ¡ª member class of a class template
    > ¡ª member class template of a class template
    > ¡ª member function template of a class template
    > can be declared by a declaration introduced by template<>
    >
    > End-STD]
    >
    > if I didn't get it wrong, we can have member class template explicit


    sorry, typo, can't

    > specialization if the outter class is not templated. Right?
    >
    >


    --
    Thanks
    Barry
    Barry, Sep 30, 2007
    #2
    1. Advertising

  3. Barry

    Barry Guest

    Barry wrote:
    > The following code compiles with VC8
    > but fails to compiles with Comeau online,
    >
    > [Error
    > "ComeauTest.c", line 7: error: explicit specialization is not allowed in
    > the
    > current scope
    > template <>
    > ^
    >
    > "ComeauTest.c", line 10: error: explicit specialization is not allowed
    > in the
    > current scope
    > template <>
    > ^
    > End-Error]
    >
    >
    > I locate the standard here:
    >
    > [STD
    > 14.7.3 Explicit specialization [temp.expl.spec]
    > An explicit specialization of any of the following:
    > ¡ª function template
    > ¡ª class template
    > ¡ª member function of a class template
    > ¡ª static data member of a class template
    > ¡ª member class of a class template
    > ¡ª member class template of a class template
    > ¡ª member function template of a class template
    > can be declared by a declaration introduced by template<>
    >
    > End-STD]
    >
    > if I didn't get it wrong, we can have member class template explicit
    > specialization if the outter class is not templated. Right?
    >
    >
    > ///////////////////////////////////////////
    >
    > struct A
    > {
    > private:
    > template <class T>
    > struct HasConvertionTo;
    >
    > template <>
    > struct HasConvertionTo<long> {};
    >
    > template <>
    > struct HasConvertionTo<bool> {};
    >
    > public:
    > template <class T>
    > operator T () const
    > {
    > return Convert(HasConvertionTo<T>()); // CT error trigger
    > }
    >
    > private:
    > template <class T>
    > T Convert(HasConvertionTo<T>) const // dispatcher
    > {
    > return Convert(T());
    > }
    >
    > long Convert(long) const
    > {
    > return 10;
    > }
    >
    > bool Convert(bool) const
    > {
    > return true;
    > }
    > };
    >
    > int main()
    > {
    > A a;
    >
    > long l = a;
    >
    > if (a) {
    > }
    > }


    14.7.3 Explicit specialization

    2.
    An explicit specialization of a member function, member class or static
    data member of a class template
    shall be declared in the namespace of which the class template is a
    member. Such a declaration may also
    be a definition. If the declaration is not a definition, the
    specialization may be defined later in the namespace
    in which the explicit specialization was declared, or in a namespace
    that encloses the one in which
    the explicit specialization was declared.

    So I think the code should be written in this way:

    [Code --
    struct A
    {
    private:
    template <class T>
    struct HasConvertionTo;

    public:
    template <class T>
    operator T () const
    {
    return Convert(HasConvertionTo<T>()); // CT error trigger
    }

    private:
    template <class T>
    T Convert(HasConvertionTo<T>) const // dispatcher
    {
    return Convert(T());
    }

    long Convert(long) const
    {
    return 10;
    }

    bool Convert(bool) const
    {
    return true;
    }
    };

    template <>
    struct A::HasConvertionTo<long> {};

    template <>
    struct A::HasConvertionTo<bool> {};
    -- End-Code]

    Which Comeau accepts.

    --
    Thanks
    Barry
    Barry, Sep 30, 2007
    #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. Dave
    Replies:
    4
    Views:
    7,651
    pdixtl
    Jun 4, 2010
  2. Andy
    Replies:
    5
    Views:
    497
    Shezan Baig
    Jan 30, 2005
  3. Ruben Campos
    Replies:
    3
    Views:
    6,452
  4. Replies:
    5
    Views:
    639
  5. Noah Roberts
    Replies:
    6
    Views:
    1,134
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page