File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
meta_program/chapter_1_specializtion_and_deduction Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 1.4.12 Default and Value Initialization
2
+ #include < string>
3
+ class A {
4
+ public:
5
+ std::string s_;
6
+ int i_;
7
+ A (){} // this will default-construct s_ but leave i_ uninitialized
8
+ // ch: in c++11, still be this?
9
+ };
10
+
11
+ /* Good as following*/
12
+ /*
13
+ * if T is as class with a default constructor, that will be used
14
+ * otherwise, the storage for T will be set to 0
15
+ * if T is an array, each element will be recursively initialized
16
+ */
17
+ template <typename T>
18
+ struct initialized_value {
19
+ T result;
20
+ initialized_value () : result() {
21
+ }
22
+ };
23
+
24
+
Original file line number Diff line number Diff line change 2
2
// #include "specialization_and_overload.h"
3
3
// #include "specialization_in_class.h"
4
4
#include " tempalte_argument_deduction.h"
5
+ #include " default_and_value_initialization.h"
5
6
using namespace std ;
6
7
7
8
int main (int argc, char ** argv) {
9
+ A a;
10
+ cout << " a.i_=" << a.i_ << " \n " ;
11
+ initialized_value<double > x;
12
+ cout << " x.result=" << x.result << " \n " ;
13
+ initialized_value<double [5 ]> y;
14
+ cout << " y.result= " ;
15
+ for (int i = 0 ;i < 5 ;++i) {
16
+ cout << y.result [i] << " " ;
17
+ }
18
+ cout << " \n " ;
19
+ initialized_value<std::string> z;
20
+ cout << " z.result = " << z.result << " \n " ;
8
21
return 0 ;
9
22
}
10
23
You can’t perform that action at this time.
0 commit comments