Template detects if T is pointer or class

后端 未结 4 499
[愿得一人]
[愿得一人] 2020-12-19 04:27

Considering the following code:

class MyClass
{
    ...
};

template 
class List
{
public:

    void insert(const Object & x)
             


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 04:58

    void insert(const Object & x)
    {
        M_insert(x, dispatcher::value> );
    }
    

    Inside List use a dispatcher

    template  class dispatcher {};
    using ObjectPtr   = dispatcher;
    using ObjectValue = dispatcher;
    

    then dispatch to M_insert:

    void M_insert(const Object &p, ObjectPtr) { // Object is a pointer }
    void M_insert(const Object &p, ObjectValue) { // Object is not a pointer }
    

    Live example here. But, I'd encourage you to determine whether you really need that and possibly fix your design accordingly.

提交回复
热议问题