forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex6_36_37_38.cpp
71 lines (58 loc) · 1.43 KB
/
ex6_36_37_38.cpp
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
//! @Alan
//!
//! Exercise 6.36:
//! Write the declaration for a function that returns a reference to an array of ten strings,
//! without using either a trailing return, decltype, or a type alias.
//!
//! Exercise 6.37:
//! Write three additional declarations for the function in the previous exercise.
//! One should use a type alias,
//! one should use a trailing return,
//! and the third should use decltype.
//! Which form do you prefer and why?
// typedef.
// Because it's easy to understand and seems similar in C which I got a bit more experience.
//!
//! Exercise 6.38:
//! Revise the arrPtr function on to return a reference to the array.
//!
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
//!
//! @brief Exercise 6.38
//! @note a function that returns a reference to an array
//!
int odd[] = {1,3,5,7,9};
int even[] = {0,2,4,6,8};
decltype(even)& arrRef(int i)
{
return (i%2)? odd : even;
}
//!
//! @brief Exercise 6.37
//! @param using decltype
//!
string arrStr[10];
decltype(arrStr)& func4(int i);
//!
//! @brief Exercise 6.37
//! @note using a trailing alias
//!
auto func3(int i) -> string (&)[10];
//!
//! @brief Exercise 6.37
//! @note using a type alias
//!
typedef string arrStrT[10];
arrStrT& func2(int i);
//!
//! @brief Exercise 6.36
//! @note a function that returns a reference to an array of ten strings
//!
string (&func(int i))[10];
int main()
{
}