File tree 3 files changed +93
-3
lines changed
3 files changed +93
-3
lines changed Original file line number Diff line number Diff line change 36
36
1 . Constructor
37
37
1 . [ Most vexing parse] ( most_vexing_parse.cpp )
38
38
1 . [ Delegating constructor] ( delegating_constructor.cpp )
39
- 1 . [ Implicitly deleted constructor] ( implicitly_deleted_constructor.cpp )
39
+ 1 . [ Implicitly defined functions] ( implicitly_defined.cpp )
40
+ 1 . [ Implicitly deleted constructor] ( implicitly_deleted_constructor.cpp )
40
41
1 . [ Constructor init object member] ( constructor_init_object_member.cpp )
41
42
1 . [ Initializer list constructor] ( initializer_list_constructor.cpp )
42
43
1 . [ Aggregate] ( aggregate.cpp )
98
99
1 . [ pair] ( pair.cpp )
99
100
1 . [ tuple] ( tuple.cpp )
100
101
1 . Smart pointers
101
- 1 . [ unique_ptr] ( unique_ptr.cpp )
102
- 1 . [ shared_ptr] ( shared_ptr.cpp )
102
+ 1 . [ unique_ptr] ( unique_ptr.cpp )
103
+ 1 . [ shared_ptr] ( shared_ptr.cpp )
104
+ 1 . [ Function that maybe returns static] ( func_ret_maybe_static.cpp )
103
105
1 . [ Interactive] ( interactive/ )
104
106
1 . [ bst_vs_heap] ( interactive/bst_vs_heap.cpp )
105
107
1 . [ chrono] ( interactive/chrono.cpp )
Original file line number Diff line number Diff line change
1
+ // https://stackoverflow.com/questions/53304666/how-to-make-a-function-that-can-return-either-a-static-or-automatic-storage-obje
2
+ // https://stackoverflow.com/questions/24344479/how-can-a-function-in-c-return-either-a-value-or-a-reference-with-minimal-copy
3
+
4
+ #include < cassert>
5
+ #include < iostream>
6
+ #include < memory>
7
+
8
+ struct C {
9
+ int i;
10
+ static int count;
11
+ C (int i) : i(i) {
12
+ std::cout << " constr" << std::endl;
13
+ count++;
14
+ }
15
+ C (const C& c) : C(c.i) {
16
+ std::cout << " copy" << std::endl;
17
+ }
18
+ ~C () {
19
+ std::cout << " destr" << std::endl;
20
+ count--;
21
+ }
22
+ };
23
+ int C::count = 0 ;
24
+
25
+ std::shared_ptr<C> func_reg_maybe_static (int i) {
26
+ static auto static_obj = std::make_shared<C>(0 );
27
+ if (i == 0 ) {
28
+ return static_obj;
29
+ } else {
30
+ return std::make_shared<C>(i);
31
+ }
32
+ }
33
+
34
+ int main () {
35
+ assert (C::count == 0 );
36
+
37
+ {
38
+ auto c (func_reg_maybe_static (0 ));
39
+ assert (c->i == 0 );
40
+ assert (C::count == 1 );
41
+ }
42
+ assert (C::count == 1 );
43
+
44
+ {
45
+ auto c (func_reg_maybe_static (0 ));
46
+ assert (c->i == 0 );
47
+ assert (C::count == 1 );
48
+ }
49
+ assert (C::count == 1 );
50
+
51
+ {
52
+ auto c (func_reg_maybe_static (1 ));
53
+ assert (c->i == 1 );
54
+ assert (C::count == 2 );
55
+ }
56
+ assert (C::count == 1 );
57
+
58
+ {
59
+ auto c (func_reg_maybe_static (2 ));
60
+ assert (c->i == 2 );
61
+ assert (C::count == 2 );
62
+ }
63
+ assert (C::count == 1 );
64
+ }
Original file line number Diff line number Diff line change
1
+ // Implicitly defined functions
2
+ //
3
+ // https://stackoverflow.com/questions/12340257/default-vs-implicit-constructor-in-c
4
+ // https://blog.mozilla.org/nfroyd/2015/08/20/explicit-is-better-than-implicit-c-implicitly-defined-member-functions/
5
+ // https://stackoverflow.com/questions/3734247/what-are-all-the-member-functions-created-by-compiler-for-a-class-does-that-hap
6
+ // https://stackoverflow.com/questions/4943958/conditions-for-automatic-generation-of-default-copy-move-ctor-and-copy-move-assi
7
+ // https://en.cppreference.com/w/cpp/language/copy_constructor
8
+ // https://en.cppreference.com/w/cpp/language/move_constructor
9
+ // https://en.cppreference.com/w/cpp/language/copy_assignment
10
+ // https://en.cppreference.com/w/cpp/language/move_assignment
11
+
12
+ #include " common.hpp"
13
+
14
+ int main () {
15
+ struct C {
16
+ C () = default ;
17
+ C (const C&) = default ;
18
+ C& operator =(C&) = default ;
19
+ C& operator =(const C&) = default ;
20
+ C (C&&) = default ;
21
+ C& operator =(C&&) = default ;
22
+ ~C () = default ;
23
+ };
24
+ }
You can’t perform that action at this time.
0 commit comments