陷入另一个模板问题:
问题:对于对象是指针的情况,我想部分地专门化一个容器类(foo),我想只专门化delete-method。应该是这样的:
lib代码
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的方法。 为什么?
感谢名单。
LIB
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
答案 0 :(得分: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..."); }
};
答案 1 :(得分: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); }
}
答案 2 :(得分:3)
我还没有看到这个解决方案,使用boost的enable_if
,is_same
和remove_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);
}
答案 3 :(得分: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...");
}
答案 4 :(得分: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..."); }
};