22--
33
44<p align =" center " >
5- <a href =" https://youtu.be/blah " ><img src =" https://img.youtube.com/vi/blah /maxresdefault.jpg " alt =" Video Thumbnail " align =" right " width =50% style =" margin : 0.5rem " ></a >
5+ <a href =" https://youtu.be/_qteFBrAKSM " ><img src =" https://img.youtube.com/vi/_qteFBrAKSM /maxresdefault.jpg " alt =" Video Thumbnail " align =" right " width =50% style =" margin : 0.5rem " ></a >
66</p >
77
88- [ ` std::function ` ] ( #stdfunction )
@@ -39,7 +39,7 @@ One thing we could do is to use **templates**. If we template the entire `Button
3939template <typename Callback>
4040class Button {
4141 public:
42- explicit Button(std::string name, Callback callback)
42+ Button(std::string name, Callback callback)
4343 : name_ {std::move(name)}, on_click_ {std::move(callback)} {}
4444
4545 void Click() const {
@@ -48,17 +48,17 @@ class Button {
4848 }
4949
5050 private:
51- std::string name_ ;
52- Callback on_click_ ;
51+ std::string name_ {} ;
52+ Callback on_click_ {} ;
5353};
5454
5555void QuitGame() { std::cout << "Quitting game...\n"; }
5656
5757int main() {
58- auto lambda_play = [ ] { std::cout << "Playing game!\n"; };
58+ const auto lambda_play = [ ] { std::cout << "Playing game!\n"; };
5959
60- Button play_button{"Play", lambda_play};
61- Button quit_button{"Quit", QuitGame};
60+ const Button play_button{"Play", lambda_play};
61+ const Button quit_button{"Quit", QuitGame};
6262
6363 play_button.Click();
6464 quit_button.Click();
@@ -127,7 +127,7 @@ Let's rewrite our `Button` class using `std::function` then. We will remove the
127127
128128class Button {
129129 public:
130- explicit Button(std::string name, std::function<void()> callback)
130+ Button(std::string name, std::function<void()> callback)
131131 : name_ {std::move(name)}, on_click_ {std::move(callback)} {}
132132
133133 void Click() const {
@@ -136,20 +136,20 @@ class Button {
136136 }
137137
138138 private:
139- std::string name_ ;
140- std::function<void()> on_click_ ;
139+ std::string name_ {} ;
140+ std::function<void()> on_click_ {} ;
141141};
142142
143143void QuitGame() { std::cout << "Quitting game...\n"; }
144144
145145int main() {
146- auto lambda_play = [ ] { std::cout << "Playing game!\n"; };
146+ const auto lambda_play = [ ] { std::cout << "Playing game!\n"; };
147147
148- Button play_button{"Play", lambda_play};
149- Button quit_button{"Quit", QuitGame};
148+ const Button play_button{"Play", lambda_play};
149+ const Button quit_button{"Quit", QuitGame};
150150
151151 // ✅ This works now! Both buttons are the EXACT SAME type.
152- std::vector<Button > buttons{play_button, quit_button};
152+ const std::vector<Button > buttons{play_button, quit_button};
153153
154154 for (const auto& button : buttons) { button.Click(); }
155155}
@@ -182,7 +182,7 @@ And what about our second problem? Now that our callbacks are wrapped in a unifo
182182
183183class Button {
184184 public:
185- explicit Button(std::string name, std::vector< std::function<void() > > callbacks)
185+ Button(std::string name, std::vector< std::function<void() > > callbacks)
186186 : name_ {std::move(name)}, on_click_callbacks_ {std::move(callbacks)} {}
187187
188188 void Click() const {
@@ -193,21 +193,21 @@ class Button {
193193 }
194194
195195 private:
196- std::string name_ ;
197- std::vector< std::function<void() > > on_click_callbacks_ ;
196+ std::string name_ {} ;
197+ std::vector< std::function<void() > > on_click_callbacks_ {} ;
198198};
199199
200200void QuitGame() { std::cout << "Quitting game...\n"; }
201201
202202int main() {
203- Button play_button{"Play", {
203+ const Button play_button{"Play", {
204204 [ ] { std::cout << "Playing game!\n"; },
205205 [ ] { std::cout << "Logging: Play was clicked.\n"; },
206206 }};
207207
208- Button quit_button{"Quit", {QuitGame}};
208+ const Button quit_button{"Quit", {QuitGame}};
209209
210- std::vector<Button > buttons{play_button, quit_button};
210+ const std::vector<Button > buttons{play_button, quit_button};
211211
212212 for (const auto& button : buttons) { button.Click(); }
213213}
@@ -293,10 +293,10 @@ $PLACEHOLDER
293293
294294class MyFunction {
295295 public:
296+ // Other constructors missing for brevity
296297 template <typename T>
297- MyFunction(T callable) {
298- callable_ = std::make_unique<CallableImpl<T>>(std::move(callable));
299- }
298+ MyFunction(T&& callable)
299+ : callable_{std::make_unique<CallableImpl<T>>(std::forward<T>(callable))} {}
300300
301301 void operator()() const { if (callable_) { callable_->Invoke(); } }
302302
@@ -309,21 +309,21 @@ class MyFunction {
309309
310310 template <typename T>
311311 struct CallableImpl : public CallableBase {
312- explicit CallableImpl(T callable) : stored_callable( std::move(callable)) {}
312+ explicit CallableImpl(T callable) : stored_callable{ std::move(callable)} {}
313313
314314 void Invoke() const override { stored_callable(); }
315315
316316 T stored_callable;
317317 };
318318
319- std::unique_ptr<CallableBase> callable_;
319+ std::unique_ptr<CallableBase> callable_{} ;
320320};
321321
322322void FreeFunction() { std::cout << "Free function!\n"; }
323323
324324int main() {
325- MyFunction func1( FreeFunction) ;
326- MyFunction func2( [] { std::cout << "Lambda!\n"; }) ;
325+ const MyFunction func1{ FreeFunction} ;
326+ const MyFunction func2{ [] { std::cout << "Lambda!\n"; }} ;
327327
328328 func1();
329329 func2();
0 commit comments