Boost :: REF

zhaozj2021-02-16  30

Sender: hhuu (I love XXX), the letter area: programming Title: Boost :: Ref News Station: BBS Shuimu Tsinghua Station (Sat May 25 02:03:27 2002)

1 Introduction

(It was really wrong. Originally, just think about why REF needs, the result example is too simple, Laws gives an opinion, check it, do not meet. Re-explain, please re-examine.)

The purpose of REF is to put the selected right to the user when a template function is available. (If you are a temporary variable, you should not use ref; otherwise, you should use REF.)

A simple idea is the method of using a function overload. However, this method cannot be implemented. If the template function can be offset, it is also a good method, but unfortunately this method doesn't even go into the ANSI standard.

2, use

Ref is used in such an occasion, a ref (), explicitly tells the compiler on the parameter. Here, if you want, it is the value transfer.

SAMPLE:

#include #include // # include using namespace std; sales namespace boost; struct QQ {INT K;};

Template INTFOO (T a) {//(add_reference ::: type ::type(a )).k = 10; typef unwrap_reference :: type & trueType; TrueType K = A ; Kk = 10; return 0;}

INT main () {qq huxw; sample:

#include #include // # include using namespace std; sales namespace boost; struct QQ {INT K;};

Template INTFOO (T a) {//(add_reference ::: type ::type(a )).k = 10; typef unwrap_reference :: type & trueType; TrueType K = A ; Kk = 10; return 0;}

INT main () {qq huxw; foo (REF (HUXW)); cout << huxw.k << Endl; huxw.k = 11; foo (huxw); cout << huxw.k << Endl; return 0; }

3, realize

Such a class, the efficiency is very important, otherwise it is to paint the tiger. ;) Quote Pass, think about it and pointer is nothing wrong. ;)

Yes, it is achieved with a pointer.

Template Class Reference_wrapper {public: typedef t type; explicit refrence_wrapper (t _ (& t) {} // Save the lower T address Operator T & () const {return * t_;} // here is smart Use a type of conversion operator. Communicate with Reference_Wapper and T & these two classes. T & GET () const {return * t_;} t * get_pointer () const {return t_;} private: T * t_;}; It can be seen that REF and CREF are not a refernerator_wapper class.

Fourth, other

This thing Boost :: REF, simple and clever, really interesting.

-

※ Modified: · hhuu at May 25 21:44:44 Modified this article · [From: 166.111.172.6] ※ Source: · BBS Shuimu Tsinghua Station Smth.edu.cn · [from: 166.111.172.6]

转载请注明原文地址:https://www.9cbs.com/read-27784.html

New Post(0)