How do I write an ADL-enabled trailing return type, or noexcept specification?

后端 未结 4 1911
野趣味
野趣味 2020-12-04 16:47

Imagine I\'m writing some container template or something. And the time comes to specialize std::swap for it. As a good citizen, I\'ll enable ADL by doing somet

4条回答
  •  醉话见心
    2020-12-04 17:01

    I think I would move it into a separate namespace

    namespace tricks {
        using std::swap;
    
        template 
        void swap(T &t, U &u) noexcept(noexcept(swap(t, u)));
    }
    
    template 
    void swap(my_template& x, my_template& y)
      noexcept(noexcept(tricks::swap(std::declval(), std::declval()))) 
    {
        using std::swap;
        swap(x.something_that_is_a_T, y.something_that_is_a_T);
    }
    

    Alternatively you can move the whole code up into tricks and delegate to there.

提交回复
热议问题