The pattern determines if T is a pointer or class

Given the following code:

class MyClass
{
    ...
};

template <typename Object>
class List
{
public:

    void insert(const Object & x)
    {
        // call when Object is MyClass
    }

    void insert(const Object & x)
    {
        // call when Object is MyClass*
    }
}

int main()
{
    MyClass a;

    List<MyClass> lst;
    List<MyClass*> plst;

    lst.insert(a);
    plst.insert(new Myclass);

    return 0;
}

How do I tell the compiler different methods based on whether the template is a class or a pointer?

How to fix the code above?

+4
source share
4 answers

You can use a combination of std::is_pointerand std::enable_if:

#include <type_traits>
#include <iostream>

class MyClass
{
};

template <typename Object>
class List
{
public:

    template<class T=Object>
    void insert(T t, typename std::enable_if<std::is_pointer<T>::value >::type* = 0) 
    {
        std::cout << "insert pointer" << std::endl;
    }

    template<class T=Object>
    void insert(T t, typename std::enable_if<!std::is_pointer<T>::value >::type* = 0) 
    {
        std::cout << "insert non-pointer" << std::endl;
    }
};

int main()
{
    MyClass a;

    List<MyClass> lst;
    List<MyClass*> plst;

    lst.insert(a);
    plst.insert(new MyClass());

    return 0;
}

Real-time example: https://ideone.com/CK8Zdo

This will allow you to insert both pointers and non pointers into the list of pointers or non pointers. If you want to limit this, you can use this:

#include <type_traits>
#include <iostream>
class MyClass
{
};

template <typename Object>
class List
{
public:

    template<class T=Object>
    void insert(T t, typename std::enable_if<std::is_same<T,Object>::value&&std::is_pointer<T>::value >::type* = 0) 
    {
        std::cout << "insert pointer" << std::endl;
    }

    template<class T=Object>
    void insert(const T& t, typename std::enable_if<std::is_same<T,Object>::value&&!std::is_pointer<T>::value >::type* = 0) 
    {
        std::cout << "insert non-pointer" << std::endl;
    }
};

int main()
{
    MyClass a;

    List<MyClass> lst;
    List<MyClass*> plst;

    lst.insert(a);
    // plst.insert(a); // compiler error

    // lst.insert(new MyClass()); // compiler error
    plst.insert(new MyClass());


    return 0;
}

Real-time example: https://ideone.com/3DtBfr

+6
source

I know that my answer is not quite about what you are asking, but maybe this can help.

, , List ( ), . . , .

:

template <typename Object>
class List
{
public:

    void insert(const Object & x)
    {
        // call when Object is MyClass
    }
};

template <typename Object>
class List<Object *>
{
public:

    void insert(Object * x)
    {
        // call when Object is MyClass*
    }
};
0
void insert(const Object & x)
{
    M_insert(x, dispatcher<std::is_pointer<Object>::value> );
}

List

template <bool B> class dispatcher {};
using ObjectPtr   = dispatcher<true>;
using ObjectValue = dispatcher<false>;

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 }

. , , , , .

0

:

template <typename Object>
class List
{
public:

    template<class C = Object>
    void insert(const C & x)
    {
        // call when Object is MyClass
        std::cout << "1" << "\n" ;
    }

    template<class P = Object*>
       void insert(P* p)
    {
        // call when Object is MyClass*
        std::cout << "2" << "\n" ;
    }
} ;

Here is a working example.

0
source

All Articles