-
Notifications
You must be signed in to change notification settings - Fork 16
/
ref.txt
72 lines (61 loc) · 4.07 KB
/
ref.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
REF
HEADER ==> #<boost/ref.hpp>
BUT ==> #Permettre à une fonction de permettre un call by
#reference ET un call by value pour un argument non-
#const :
# - FONC(TYPE) force un call by value
# - FONC(TYPE&) force un call by reference
# - FONC(const TYPE&) permet les deux, mais rend
# l'argument const dans FONC.
# - overload FONC(TYPE) et FONC(TYPE&) ne fonctionne
# pas.
# - overload FONC(const TYPE&) et FONC(TYPE&) peu
# pratique et répétition de code.
#Intérêt :
# - permettre au caller de faire un call by reference
# s'il le veut (performance, objet non copiable ou
# objet avec un state), sans interdire un call by
# value également.
#reference_wrapper<T> wrappe un référence.
#Mise en oeuvre :
# - le caller donne comme argument ARG s'il veut un
# call by value, et [c]ref(ARG) s'il veut un call by
# reference, et :
# 1) Ou :
# - le callee overloade une fonction prenant TYPE
# (effectuant un call by value), et une autre
# prenant reference_wrapper<TYPE> (effectuant un
# call by reference, et utilisant(TYPE&) ou .get())
# 2) Ou (mieux et évite répétition de code) :
# - le callee utilise un template, et :
# - manipule T via unwrap_ref(T) : modifie la
# référence de l'argument si [c]ref(ARG), sa
# copie sinon.
# - peut aussi traiter T (by value ou by
# reference) en fonction de
# is_reference_wrapper<T>::value.
# - le TYPE originel peut être connu grâce à
# unwrap_reference<...>::type, que T soit
# wrappé ou non.
reference_wrapper<TYPE> #Classe wrappant une référence.
#Est CopyConstructible et Assignable
REFERENCE_WRAPPER(VAR) #Wrappe la référence VAR (VAL possible si TYPE est
#const)
(TYPE&) REFERENCE_WRAPER
REFERENCE_WRAPPER.get() #Renvoie la référence wrappée.
REFERENCE_WRAPPER.
get_pointer() #Renvoie un pointeur vers la référence wrappée.
reference_wrapper<TYPE>
::type #Typedef depuis TYPE.
ref(VAR) #Renvoie reference_wrapper<TYPE>(VAR)
cref(const VAL) #Renvoie reference_wrapper<TYPE const>(VAL).
#Permet d'utiliser une VAL.
unwrap_reference<TYPE>
::type #typedef depuis TYPE::type si TYPE est
#reference_wrapper, depuis TYPE sinon
unwrap_ref(VAR) #Renvoie (unwrap_reference<TYPE>::type&) VAR
is_reference_wrapper
<TYPE>::value #true si TYPE est un reference_wrapper<...>
PORTABILITÉ ==> #Ne marche pas avec MSVC++