How to make template rvalue reference parameter ONLY bind to rvalue reference?

前端 未结 5 2052
心在旅途
心在旅途 2020-12-01 08:59

I\'m writing a network library and use move semantics heavily to handle ownership for file descriptors. One of my class wishes to receive file descriptor wrappers of other k

5条回答
  •  一整个雨季
    2020-12-01 09:30

    You can restrict T to not be an lvalue reference, and thus prevent lvalues from binding to it:

    #include 
    
    struct OwnershipReceiver
    {
      template ::value
                >::type
               >
      void receive_ownership(T&& t)
      {
         // taking file descriptor of t, and clear t
      }
    };
    

    It might also be a good idea to add some sort of restriction to T such that it only accepts file descriptor wrappers.

提交回复
热议问题