One-liner for RAII on non pointer?

前端 未结 6 1772
小蘑菇
小蘑菇 2020-12-24 08:31

Related topic

std::unique_ptr, deleters and the Win32 API

To use a Win32 Handle as a RAII, I can use the following line

std:         


        
6条回答
  •  一整个雨季
    2020-12-24 09:26

    Kerrek SB answered in the comments and it was exactly what I was looking for!

    template  
    struct stateless_deleter 
    {
        typedef T pointer; 
    
        void operator()(T x) 
        { 
            Deleter(x); 
        } 
    };
    std::unique_ptr> listenSocket(socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP));
    

提交回复
热议问题