Specializing Member Function Template of Class Template?

Discussion in 'C++' started by Simon G Best, Dec 29, 2006.

  1. Simon G Best

    Simon G Best Guest

    Hello!

    I'm trying to specialize a member function template of a class template,
    like this:-

    template<typename T> class thingy {
    public:
    template<typename U> T f (const U &) const;
    };

    // The general case:-
    template<typename T> template<typename U> T thingy<T>::f
    (const U &x) const {
    return T(x);
    }

    // The special case:-
    template<typename T> template<> T thingy<T>::f<T>
    (const T &x) const {
    return x;
    }

    But it's not working :-( I've tried a few variations (such as throwing
    in a 'template' to disambiguate), but my compilers (G++ 4.1.2 and G++
    3.3.6) just don't seem to like it. With the above C++, I'm getting the
    following out of G++ 4.1.2:-

    templates.cpp:13: error: invalid explicit specialization before ‘>’ token
    templates.cpp:13: error: enclosing class templates are not explicitly
    specialized
    templates.cpp:14: error: template-id ‘f<T>’ for ‘T thingy<T>::f(const
    T&) const’ does not match any template declaration
    templates.cpp:14: error: invalid function declaration

    :-(

    What am I doing wrong?

    Simon

    --
    What happens if I mention Leader Kibo in my .signature?
     
    Simon G Best, Dec 29, 2006
    #1
    1. Advertising

  2. Simon G Best

    kwikius Guest

    Simon G Best wrote:


    > What am I doing wrong?


    Beacause the *parent* class is a template this is not allowed IIRC,
    because someone could specialise the parent class which could cause
    ambiguities. That is the rationale or something like it.

    If you have boost you can try the enable_if technique --->

    (Actually you could easily make your own versions of enable_if and
    is_same if you dont have boost , but do put attribution to the
    designers of course):

    #include <boost/utility/enable_if.hpp>
    //http://www.boost.org/libs/utility/enable_if.html
    #include <boost/type_traits/is_same.hpp>
    //http://www.boost.org/doc/html/boost_typetraits.html
    #include <iostream>

    Not sure if it works on older gcc though. Tested on VC7.1 and gcc4.1.

    //--------------------------

    template<typename T> class thingy {
    public:

    /*template <typename U>
    T f( U const & x) const;*/

    template<typename U>
    typename boost::enable_if<
    boost::is_same<T,U>,
    T
    >::type

    f (const U &) const;

    template<typename U>
    typename boost::disable_if<
    boost::is_same<T,U>,
    T
    >::type

    f (const U &) const;
    };


    // The general case:-
    template<typename T>
    template<typename U>
    typename boost::disable_if<
    boost::is_same<T,U>,
    T
    >::type

    thingy<T>::f (const U &x) const {

    std::cout << "general case\n";
    return T(x);
    }
    // special case
    template<typename T>
    template<typename U>
    typename boost::enable_if<
    boost::is_same<T,U>,
    T
    >::type

    thingy<T>::f
    (const U &x) const {
    std::cout << "special case\n";
    return x;
    }


    int main()
    {
    thingy <int> n;
    n.f(1);
    n.f(2.0);
    }

    regards
    Andy Little
     
    kwikius, Dec 29, 2006
    #2
    1. Advertising

  3. Simon G Best

    Simon G Best Guest

    kwikius wrote:
    >
    > Beacause the *parent* class is a template this is not allowed IIRC,
    > because someone could specialise the parent class which could cause
    > ambiguities. That is the rationale or something like it.
    >
    > If you have boost you can try the enable_if technique --->

    [...]

    Thanks, but I don't think changing the class template itself is
    justified, as my intended specialization isn't part of the interface,
    but only for dealing with implementation details.

    What I could do, though, is something like the following:-

    [Start C++ here.]
    #include <iostream>

    template<typename T> class thingy {
    public:
    template<typename U> T f (const U &) const;
    };

    namespace {

    /* Here's how I can get the kind of specialization I'm after.
    */

    // The general case:-
    template<typename T, typename U> class thingy_f_implementation {
    public:
    static T f (const U &x) {
    ::std::clog << "General." << ::std::endl;
    return T(x);
    }
    };

    // The special case:-
    template<typename T> class thingy_f_implementation<T, T> {
    public:
    static T f (const T &x) {
    ::std::clog << "Special." << ::std::endl;
    return x;
    }
    };
    }

    template<typename T> template<typename U> T thingy<T>::f
    (const U &x) const {
    return thingy_f_implementation<T, U>::f(x);
    }

    /* And just to prove it works:-
    */

    int main () {
    thingy<int> x;
    x.f(float(2.3));
    x.f(int(42));
    return 0;
    }
    [End C++ here.]

    But I'd rather do it more directly, as indicated in my original post. I
    hoped it was just that I'd messed up the syntax, or something. But if,
    as you say, it just can't be done that way, then I suppose taking the
    scenic route (as above) would have to be it.

    But thanks for your suggestion anyway :)

    Simon

    --
    What happens if I mention Leader Kibo in my .signature?
     
    Simon G Best, Dec 29, 2006
    #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. Jeff
    Replies:
    2
    Views:
    438
  2. Rahul
    Replies:
    8
    Views:
    413
    Lionel B
    Jul 16, 2007
  3. Igor R.
    Replies:
    4
    Views:
    495
    Igor R.
    Nov 11, 2009
  4. John
    Replies:
    3
    Views:
    354
    James Kanze
    Aug 17, 2010
  5. m0shbear
    Replies:
    2
    Views:
    359
Loading...

Share This Page