C++ template partial specialization - specializing one member function only

后端 未结 5 1287
执念已碎
执念已碎 2020-12-28 21:16

Bumped into another templates problem:

The problem: I want to partially specialize a container-class (foo) for the case that the objects are pointers, and i want to

相关标签:
5条回答
  • 2020-12-28 21:40

    Another solution. Use the auxiliary function deleteSomeHelp.

    template <typename T>
    class foo {
     public:    
       void addSome    (T o) { printf ("adding that object..."); 
       template<class R>
       void deleteSomeHelp (R   o) { printf ("deleting that object..."); }};
       template<class R>
       void deleteSomeHelp (R * o) { printf ("deleting that PTR to an object..."); }};
       void deleteSome (T o) { deleteSomeHelp(o); }
    }    
    
    0 讨论(0)
  • 2020-12-28 21:45

    I haven't seen this solution yet, using boost's enable_if, is_same and remove_pointer to get two functions in a class, without any inheritance or other cruft.

    See below for a version using only remove_pointer.

    #include <boost\utility\enable_if.hpp>
    #include <boost\type_traits\is_same.hpp>
    #include <boost\type_traits\remove_pointer.hpp>
    
    template <typename T>
    class foo
    {
    public:
        typedef typename boost::remove_pointer<T>::type T_noptr;
    
        void addSome    (T o) { printf ("adding that object..."); }
    
        template<typename U>
        void deleteSome (U o, typename boost::enable_if<boost::is_same<T_noptr, U>>::type* dummy = 0) { 
            printf ("deleting that object..."); 
        }
        template<typename U>
        void deleteSome (U* o, typename boost::enable_if<boost::is_same<T_noptr, U>>::type* dummy = 0) { 
            printf ("deleting that PTR to that object..."); 
        }
    };
    

    A simplified version is:

    #include <cstdio>
    #include <boost\type_traits\remove_pointer.hpp>
    
    template <typename T>
    class foo
    {
    public:
        typedef typename boost::remove_pointer<T>::type T_value;
    
        void addSome    (T o) { printf ("adding that object..."); }
    
        void deleteSome (T_value& o) { // need ref to avoid auto-conv of double->int
            printf ("deleting that object..."); 
        }
    
        void deleteSome (T_value* o) { 
            printf ("deleting that PTR to that object..."); 
        }
    };
    

    And it works on MSVC 9: (commented out lines that give errors, as they are incorrect, but good to have for testing)

    void main()
    {
       foo<int> x;
       foo<int*> y;
    
       int a;
       float b;
    
       x.deleteSome(a);
       x.deleteSome(&a);
       //x.deleteSome(b); // doesn't compile, as it shouldn't
       //x.deleteSome(&b);
       y.deleteSome(a);
       y.deleteSome(&a);
       //y.deleteSome(b);
       //y.deleteSome(&b);
    }
    
    0 讨论(0)
  • 2020-12-28 21:52

    Second solution (correct one)

    template <typename T>
    class foo
    {
    public:
        void addSome    (T o) { printf ("adding that object..."); } 
        void deleteSome(T o) { deleteSomeHelper<T>()(o); }
    protected:
        template<typename TX> 
        struct deleteSomeHelper { void operator()(TX& o) { printf ("deleting that object..."); } };
        template<typename TX> 
        struct deleteSomeHelper<TX*> { void operator()(TX*& o) { printf ("deleting that PTR to an object..."); } };
    };
    

    This solution is valid according to Core Issue #727.


    First (incorrect) solution: (kept this as comments refer to it)

    You cannot specialize only part of class. In your case the best way is to overload function deleteSome as follows:

    template <typename T>
    class foo
    {
    public:
        void addSome    (T o) { printf ("adding that object..."); }
        void deleteSome (T o) { printf ("deleting that object..."); }
        void deleteSome (T* o) { printf ("deleting that object..."); }
    };
    
    0 讨论(0)
  • 2020-12-28 21:55

    Create base class for single function deleteSome

    template<class T>
    class base {
    public:
      void deleteSome (T o) { printf ("deleting that object..."); }
    }
    

    Make partial specialization

    template<class T>
    class base<T*> {
    public:
      void deleteSome (T * o) { printf ("deleting that PTR to an object..."); }
    }
    

    Use your base class

    template <typename T>
    class foo : public base<T> {
     public:    
       void addSome    (T o) { printf ("adding that object..."); 
    }    
    
    0 讨论(0)
  • 2020-12-28 21:59

    You can use inheritance to get this to work :

    template <typename T>
    class foobase
    {
    public:
        void addSome    (T o) { printf ("adding that object..."); }
        void deleteSome (T o) { printf ("deleting that object..."); }
    };
    
    template <typename T>
    class foo : public foobase<T>
    { };
    
    template <typename T>
    class foo <T *> : public foobase<T>
    {
    public:
        void deleteSome (T* o) { printf ("deleting that PTR to an object..."); }
    };
    
    0 讨论(0)
提交回复
热议问题