Using unique_ptr to control a file descriptor

前端 未结 9 2111
無奈伤痛
無奈伤痛 2020-12-10 05:29

In theory, I should be able to use a custom pointer type and deleter in order to have unique_ptr manage an object that is not a pointer. I tried the following c

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 05:43

    Making Nicol Bolas class more general:

    template
    class Handle
        {
        public:
            Handle(T handle):m_handle(handle){}
    
            Handle(std::nullptr_t):m_handle(null_val){}
    
            operator T(){return m_handle;}
    
            bool operator==(const Handle& other) const
                {return other.m_handle==m_handle;}
    
        private:
            T m_handle;
        };
    
    typedef Handle FileDescriptor;
    typedef Handle GlResource; // according to http://stackoverflow.com/questions/7322147/what-is-the-range-of-opengl-texture-id
    // ...
    

    I am not sure if I should have default template parameter values or not.

提交回复
热议问题