Skip to content

Commit f45f0cb

Browse files
committed
Add returning object reference
Replace Perfect Forwarding with retuning reference to an object as this is the more often used case needed in a refcard.
1 parent b414faa commit f45f0cb

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

cpp_param_ref.org

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -204,51 +204,55 @@ void Deduce(doc::Properties& p) {
204204
}
205205
#+END_SRC
206206

207-
* ~In Out~
207+
* ~Out~ Object Reference
208208

209209
#+BEGIN_EXPORT latex
210-
\invisible{hi}\vspace{-2em} % remove unnecessary vspace due to minted
211-
\hfill\begin{tabular}{@{}p{0.4\linewidth}@{}} % align horizontally in page
212-
\vspace{-3em} % align vertically with section title
210+
\invisible{hi}\vspace{-1.5em}
211+
\hfill\begin{tabular}{@{}p{0.4\linewidth}@{}}
212+
\vspace{-3em}
213213
#+END_EXPORT
214214

215215
#+BEGIN_SRC c++
216-
void f(T&);
216+
T& f();
217+
const T& f();
217218
#+END_SRC
218219

219220
#+LATEX: \end{tabular}
220221

221-
Objects for /reading/ and /writing/: *lvalue reference*
222+
Return *lvalue reference* to /object outliving function and caller/
222223

223224
#+BEGIN_SRC c++
224225
// example
225-
void AdjustMargins(std::vector<Margin>& margins) {
226-
}
226+
struct Application {
227+
Document& GetDocument { return m_pdf; } // covariant return type
228+
PDFDocument m_pdf;
229+
}; // app outlives GetDocument() and temp() calls
230+
void PrintDoc(Application& app) { app.GetDocument().Print(); }
231+
int main() { Application app{str_pdf_path}; PrintDoc(app); }
227232
#+END_SRC
228233

229-
* Perfect Forwarding
234+
* ~In Out~
230235

231236
#+BEGIN_EXPORT latex
232-
\invisible{hi}\vspace{-1.5em}
233-
\hfill\begin{tabular}{@{}p{0.4\linewidth}@{}}
234-
\vspace{-3em}
237+
\invisible{hi}\vspace{-2em} % remove unnecessary vspace due to minted
238+
\hfill\begin{tabular}{@{}p{0.4\linewidth}@{}} % align horizontally in page
239+
\vspace{-3em} % align vertically with section title
235240
#+END_EXPORT
236241

237242
#+BEGIN_SRC c++
238-
template <typename T>
239-
void f(T&&);
243+
void f(T&);
240244
#+END_SRC
241245

242246
#+LATEX: \end{tabular}
243247

244-
Pass parameter retaining lvalue/rvalue-/ness/: *forwarding reference*
248+
Objects for /reading/ and /writing/: *lvalue reference*
245249

246250
#+BEGIN_SRC c++
247-
template <typename T>
248-
void SetTiel(T&& title) {
249-
internal::SetTitle(std::forward<T>(title));
251+
// example
252+
void AdjustMargins(std::vector<Margin>& margins) {
250253
}
251254
#+END_SRC
252255

256+
253257
#+LATEX: \vspace{0.75em}
254258
*Reference*: [[https://github.com/CppCon/CppCon2014/blob/master/Presentations/Back%2520to%2520the%2520Basics!%2520Essentials%2520of%2520Modern%2520C%252B%252B%2520Style/Back%2520to%2520the%2520Basics!%2520Essentials%2520of%2520Modern%2520C%252B%252B%2520Style%2520-%2520Herb%2520Sutter%2520-%2520CppCon%25202014.pdf][Essentials of Modern C++ Style, /Herb Sutter/]]

0 commit comments

Comments
 (0)