C++模板部分特化 - 仅特化一个成员函数

发布于 2024-08-12 05:35:42 字数 1681 浏览 2 评论 0原文

遇到另一个模板问题:

问题:我想部分专门化容器类(foo)来处理对象是指针的情况,并且我只想专门化删除方法。应该如下所示:

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 的方法。 为什么 ?

谢谢。


解决方案

based on tony's answer here the fully compilable stuff


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

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 specialize only the delete-method. Should look like this:

The lib code

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

The user code

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

myptrs.addSome (new myclass());

This results into the compiler telling me that myptrs doesnt have a method called addSome.
Why ?

Thanx.


Solution

based on tony's answer here the fully compilable stuff


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

user

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

咿呀咿呀哟 2024-08-19 05:35:42

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

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,该解决方案是有效的问题#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..."); }
};

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..."); }
};
可遇━不可求 2024-08-19 05:35:42

另一个解决方案。使用辅助函数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); }
}    

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); }
}    
万水千山粽是情ミ 2024-08-19 05:35:42

我还没有看到这个解决方案,使用boost的 启用_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);
}

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);
}
鸠书 2024-08-19 05:35:42

为单个函数创建基类 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..."); 
}    

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..."); 
}    
櫻之舞 2024-08-19 05:35:42

您可以使用继承来使其发挥作用:

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

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..."); }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文