Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的智能指针+;使用模板 我试图用C++实现一个智能指针(基本上是唯一指针),用模板来理解。p>_C++_Templates_Reference_Move - Fatal编程技术网

C+中的智能指针+;使用模板 我试图用C++实现一个智能指针(基本上是唯一指针),用模板来理解。p>

C+中的智能指针+;使用模板 我试图用C++实现一个智能指针(基本上是唯一指针),用模板来理解。p>,c++,templates,reference,move,C++,Templates,Reference,Move,这就是我写的代码 using namespace std; template<typename T> class smartPointer { private: T *mPtr; public: smartPointer(T* init=nullptr):mPtr(init) { cout<<"Inside ctr"<<endl; }

这就是我写的代码

using namespace std;

template<typename T>
class smartPointer
{
    private:
        T *mPtr;
    public:
        smartPointer(T* init=nullptr):mPtr(init)
        {
            cout<<"Inside ctr"<<endl;
        }


        //silence the default ctr
        smartPointer() = delete;

        //disable copy ctr and copy assignment
        smartPointer  (const smartPointer& other) = delete;
        smartPointer& operator=(const smartPointer& other) = delete;

        //implement move ctr and move assignment
        smartPointer  (smartPointer&& other)
        {
            cout<<"Inside move ctr "<<endl;
            mPtr = other.mPtr;
            other.mPtr = nullptr;
        }
        smartPointer& operator=(smartPointer&& other)
        {

            cout<<"Inside move = before "<<endl;
            if(&other != this)
            {
                mPtr = other.mPtr;
                other.mPtr = nullptr;

                cout<<"Inside move = after "<<endl;
            }
        return *this;
        }

        //deference
        T& operator*()
        {
            return *mPtr;
        }

        //arrow  access operator
        T* operator->()
        {   
            return mPtr;
        }

        ~smartPointer()
        {   
            cout<<"Inside ~dtr"<<endl;
            if(mPtr != nullptr)
            delete mPtr;
        }

};



int main()
{

     smartPointer<int> sptr(new int);

     // Even smartPointer<int> sptr(new int[20]); too works

     *sptr = 10;
     cout<<"Value pointed by the pointer  is "<<*sptr<<endl;     

    smartPointer<int> sptr2(new int);

//  sptr2 = sptr; // complains as expected
    sptr2 = move(sptr); // works well

    /*
    How to 

    smartPointer<int[]>  sptr(new int[20]); 
    */

    return 0;
}
使用名称空间std;
模板
类智能指针
{
私人:
T*mPtr;
公众:
智能指针(T*init=nullptr):mPtr(init)
{

cout您可以添加
运算符[]
到基本模板,并使用SFINAE确保它仅在类型为数组类型时才编译。您可以执行相同的操作,以确保
operaotr*
operaotr->
仅编译非数组版本。然后,可以使用标记分派来调用正确的
delete
表达式

template<typename T>
class smartPointer
{
    private:
        T *mPtr;

        del_member(std::true_type) { delete[] mPtr; }
        del_member(std::false_type) { delete mPtr; }

    public:
        // Removed you code for brevity.

        //deference
        auto operator*() -> std::enable_if_t<!std::is_array<T>::value, T&>
        {
            return *mPtr;
        }

        //arrow  access operator
        auto operator->() -> std::enable_if_t<!std::is_array<T>::value, T*>
        {   
            return mPtr;
        }

        auto operator[](std::size_t idx) -> std::enable_if_t<std::is_array<T>::value, T&>
        {   
            return mPtr[idx];
        }

        ~smartPointer()
        {   
            del_member(std::is_array<T>{});
        }
};

非常感谢。我接受你的回答。谢谢:)学到了一个新东西SIFNAE,谢谢你的快速回复:)你能在构造函数中帮助我吗,我得到了这个错误错误:调用“smartPointer::smartPointer(int*)”smartPointer sptr(new int[20])没有匹配的函数@ Balu见我的编辑。BUF如果你有更多的问题,发布另一个问题。所以不是一个Fuim.AtdialLoT在C++ 11中是不可用的。@ Balu我写的关于<代码> Enable的,如果也适用于<代码>条件> < /C> >。
typename std::enable_if<CONDITION, TYPE>::type
using ptr_type = std::conditional_t<std::is_array<T>::value, std::decayt_t<T>, std::add_pointer_t<T>>;

smartPointer(ptr_type init)