Full specialization of a member function template of a class template

Discussion in 'C++' started by Dave, May 5, 2004.

  1. Dave

    Dave Guest

    Hello all,

    I am trying to create a full specialization of a member function template of
    a class template. I get the following errors:

    Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
    Line 29: 'bar' : unable to match function definition to an existing
    declaration

    What am I doing wrong?

    Thanks,
    Dave


    #include <iostream>

    using namespace std;

    template <typename T1>
    class foo
    {
    public:
    template <typename T2>
    void bar(const T2 &param);
    };

    template <typename T1>
    template <typename T2>
    void foo<T1>::bar(const T2 &param)
    {
    static_cast<void>(param);

    cout << "Point 1" << endl;
    }

    template <typename T1>
    template <>
    void foo<T1>::bar<double>(const double &param)
    {
    static_cast<void>(param);

    cout << "Point 2" << endl;
    }

    int main()
    {
    foo<int> var;

    var.bar(4.5);
    }
     
    Dave, May 5, 2004
    #1
    1. Advertising

  2. "Dave" <> wrote...
    >
    > Hello all,
    >
    > I am trying to create a full specialization of a member function template

    of
    > a class template. I get the following errors:
    >
    > Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
    > Line 29: 'bar' : unable to match function definition to an existing
    > declaration
    >
    > What am I doing wrong?


    It is expressly prohibited. If you want to explicitly specialise
    a member template, the enclosing class has to be specialised as well.

    See 14.7.3/17.

    >
    > Thanks,
    > Dave
    >
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > template <typename T1>
    > class foo
    > {
    > public:
    > template <typename T2>
    > void bar(const T2 &param);
    > };
    >
    > template <typename T1>
    > template <typename T2>
    > void foo<T1>::bar(const T2 &param)
    > {
    > static_cast<void>(param);
    >
    > cout << "Point 1" << endl;
    > }
    >
    > template <typename T1>
    > template <>
    > void foo<T1>::bar<double>(const double &param)
    > {
    > static_cast<void>(param);
    >
    > cout << "Point 2" << endl;
    > }
    >
    > int main()
    > {
    > foo<int> var;
    >
    > var.bar(4.5);
    > }
    >
    >
     
    Victor Bazarov, May 5, 2004
    #2
    1. Advertising

  3. Dave

    John Carson Guest

    "Victor Bazarov" <> wrote in message
    news:UfXlc.24994$_41.1789783@attbi_s02
    > "Dave" <> wrote...
    >>
    >> Hello all,
    >>
    >> I am trying to create a full specialization of a member function
    >> template of a class template. I get the following errors:
    >>
    >> Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
    >> Line 29: 'bar' : unable to match function definition to an existing
    >> declaration
    >>
    >> What am I doing wrong?

    >
    > It is expressly prohibited. If you want to explicitly specialise
    > a member template, the enclosing class has to be specialised as well.
    >
    > See 14.7.3/17.
    >


    In this case a workaround is to add the specialised member function to the
    class declaration:

    template <typename T1>
    class foo
    {
    public:
    template <typename T2>
    void bar(const T2 &param);

    void bar(const double &param);
    };

    template <typename T1>
    void foo<T1>::bar(const double &param)
    {
    static_cast<void>(param);
    cout << "Point 2" << endl;
    }


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, May 5, 2004
    #3
  4. Dave

    Buster Guest

    Re: Full specialization of a member function template of a classtemplate

    Dave wrote:
    > Hello all,
    >
    > I am trying to create a full specialization of a member function template of
    > a class template.


    From the standard (towards the end of 14.7.3),

    "In an explicit specialization declaration for a member of a class
    template or a member template that appears in namespace scope, the
    member template and some of its enclosing class templates may remain
    unspecialized, except that the declaration shall not explicitly
    specialize a class member template if its enclosing class templates
    are not explicitly specialized as well."

    Crystal clear?

    You can't explicitly specialize a member template of a class template,
    but you can explicitly specialize a member template of an explicit
    specialization of a class template.

    > I get the following errors:
    > Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
    > Line 29: 'bar' : unable to match function definition to an existing
    > declaration


    That's pretty crap. G++ 3.3.1 gives "enclosing class templates are not
    explicitly specialized".

    --
    Regards,
    Buster.
     
    Buster, May 5, 2004
    #4
  5. Dave

    pdixtl

    Joined:
    Jun 4, 2010
    Messages:
    1
    would you know how to explicitly specialize a member template when template argument for the member is different than template argument of the class?
     
    pdixtl, Jun 4, 2010
    #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.
Similar Threads
  1. Ruben Campos
    Replies:
    3
    Views:
    6,474
  2. BigMan
    Replies:
    1
    Views:
    526
  3. Replies:
    4
    Views:
    13,046
    Victor Bazarov
    Jun 15, 2005
  4. Joseph Turian
    Replies:
    4
    Views:
    598
    John Carson
    Mar 20, 2006
  5. toton
    Replies:
    2
    Views:
    339
    toton
    Feb 22, 2008
Loading...

Share This Page