12

碰到另一个模板问题:

问题:对于对象是指针的情况,我想部分专门化一个容器类(foo),并且我只想专门化删除方法。应该是这样的:

库代码

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

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

用户代码

foo<myclass> myclasses;
foo<myclass*> myptrs;

myptrs.addSome (new myclass());

这导致编译器告诉我 myptrs 没有名为 addSome 的方法。为什么 ?

谢谢。


解决方案

基于托尼的回答here完全可编译的东西


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..."); }
};

用户

foo<int>    fi;
foo<int*>   fpi;

int         i = 13;

fi.addSome (12);            
fpi.addSome (&i);

fpi.deleteSome (12);        // compiler-error: doesnt work
fi.deleteSome (&i);         // compiler-error: doesnt work
fi.deleteSome (12);         // foobase::deleteSome called
fpi.deleteSome (&i);        // foo<T*>::deleteSome called
4

5 回答 5

11

第二种解决方案(正确的一种)

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..."); } };
};

此解决方案根据Core Issue #727有效。


第一个(不正确的)解决方案:(保留这个作为评论引用它)

你不能只专攻部分课程。deleteSome在您的情况下,最好的方法是按如下方式重载函数:

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..."); }
};
于 2009-11-18T17:51:44.880 回答
10

另一种解决方案。使用辅助功能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); }
}    
于 2009-11-18T18:21:09.517 回答
3

我还没有看到这个解决方案,使用 boost'senable_ifis_sameremove_pointer一个类中获取两个函数,没有任何继承或其他麻烦。

有关仅使用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..."); 
    }
};

一个简化的版本是:

#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..."); 
    }
};

它适用于 MSVC 9:(注释掉了错误的行,因为它们不正确,但很适合测试)

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);
}
于 2009-11-18T18:52:34.063 回答
2

为单个函数创建基类deleteSome

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

进行部分专业化

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

使用你的基类

template <typename T>
class foo : public base<T> {
 public:    
   void addSome    (T o) { printf ("adding that object..."); 
}    
于 2009-11-18T18:10:06.247 回答
1

您可以使用继承来使其工作:

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..."); }
};
于 2009-11-18T17:54:55.210 回答