function object研究之十二 reference_wrapper

來源:互聯網
上載者:User

reference_wrapper模板:(ref.hpp檔案中)

template<class T> class reference_wrapper{ public:    typedef T type;#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, < 1300 )    explicit reference_wrapper(T& t): t_(&t) {}#else    explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}#endif    operator T& () const { return *t_; }    T& get() const { return *t_; }    T* get_pointer() const { return t_; }private:    T* t_;};

有了前面分析addressof的基礎,理解此模板很簡單。

功能就是:

1.儲存T& t 的地址到內部指標成員T* t_中。

2.提供get方法擷取T&

3.提供get_pointer擷取T*

再看看相關的幾個模板函數:

template<class T> inline reference_wrapper<T> BOOST_REF_CONST ref(T & t){     return reference_wrapper<T>(t);}template<class T> inline reference_wrapper<T const> BOOST_REF_CONST cref(T const & t){    return reference_wrapper<T const>(t);}

第一個將t封裝成reference_wrapper<T>

第二個將t封裝成reference_wrapper<T const>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.