回答編集履歴
4
修正
test
CHANGED
@@ -41,6 +41,8 @@
|
|
41
41
|
template<typename T, size_t Dim, typename... Args>
|
42
42
|
|
43
43
|
NPtr<T, Dim>::type AllocDim(Args... _width){
|
44
|
+
|
45
|
+
static_assert(Dim == sizeof...(_width), "sizeof...(_width) must equal Dim");
|
44
46
|
|
45
47
|
std::array<size_t, Dim> width = {_width...};
|
46
48
|
|
3
修正
test
CHANGED
@@ -6,13 +6,17 @@
|
|
6
6
|
|
7
7
|
---
|
8
8
|
|
9
|
-
C++においてはテンプレートによって作れ
|
9
|
+
C++においてはテンプレートによって作れました。
|
10
10
|
|
11
11
|
が、前述のようにコンパイル時に`Dim`が決定されている必要があったりと実用的かは微妙です
|
12
12
|
|
13
13
|
|
14
14
|
|
15
15
|
```c++
|
16
|
+
|
17
|
+
#include <array>
|
18
|
+
|
19
|
+
|
16
20
|
|
17
21
|
template <typename T, size_t Dim>
|
18
22
|
|
@@ -34,9 +38,11 @@
|
|
34
38
|
|
35
39
|
|
36
40
|
|
37
|
-
template<typename T, size_t Dim>
|
41
|
+
template<typename T, size_t Dim, typename... Args>
|
38
42
|
|
39
|
-
NPtr<T, Dim>::type AllocDim(s
|
43
|
+
NPtr<T, Dim>::type AllocDim(Args... _width){
|
44
|
+
|
45
|
+
std::array<size_t, Dim> width = {_width...};
|
40
46
|
|
41
47
|
size_t elements = 1, points = 0;
|
42
48
|
|
@@ -84,9 +90,11 @@
|
|
84
90
|
|
85
91
|
}
|
86
92
|
|
93
|
+
size_t last = width[Dim-1];
|
94
|
+
|
87
95
|
for(size_t i = 0; it < (void**)data; i++) {
|
88
96
|
|
89
|
-
*it++ = (void*)(data + i * sizeof(T));
|
97
|
+
*it++ = (void*)(data + last * i * sizeof(T));
|
90
98
|
|
91
99
|
}
|
92
100
|
|
@@ -96,4 +104,10 @@
|
|
96
104
|
|
97
105
|
|
98
106
|
|
107
|
+
int main(){
|
108
|
+
|
109
|
+
char*** ptr = AllocDim<char, 3>(3,2,2);
|
110
|
+
|
111
|
+
}
|
112
|
+
|
99
113
|
```
|
2
修正
test
CHANGED
@@ -13,8 +13,6 @@
|
|
13
13
|
|
14
14
|
|
15
15
|
```c++
|
16
|
-
|
17
|
-
// 作りかけ
|
18
16
|
|
19
17
|
template <typename T, size_t Dim>
|
20
18
|
|
@@ -52,27 +50,43 @@
|
|
52
50
|
|
53
51
|
points -= 1;
|
54
52
|
|
53
|
+
size_t size = elements * sizeof(T) + points * sizeof(void**);
|
55
54
|
|
56
|
-
|
57
|
-
void* result = malloc(
|
55
|
+
void* result = malloc(size);
|
58
56
|
|
59
57
|
if(!result)
|
60
58
|
|
61
|
-
return
|
59
|
+
return nullptr;
|
62
60
|
|
63
61
|
void** it = (void**)result;
|
64
62
|
|
65
63
|
char* data = (char*)(it+points);
|
66
64
|
|
67
|
-
|
65
|
+
size_t memo = 0;
|
68
66
|
|
67
|
+
size_t t = 1;
|
68
|
+
|
69
|
+
for(size_t i = 0; i < Dim-2; i++) {
|
70
|
+
|
71
|
+
size_t w = width[i], diff = width[i+1];
|
72
|
+
|
73
|
+
memo += w;
|
74
|
+
|
75
|
+
t *= w;
|
76
|
+
|
69
|
-
|
77
|
+
void** base = it;
|
78
|
+
|
79
|
+
for(size_t l = 0; l < t; l++) {
|
80
|
+
|
81
|
+
*it++ = (void*)(base + memo + l * diff);
|
82
|
+
|
83
|
+
}
|
70
84
|
|
71
85
|
}
|
72
86
|
|
73
|
-
for(size_t i = 0; i <
|
87
|
+
for(size_t i = 0; it < (void**)data; i++) {
|
74
88
|
|
75
|
-
|
89
|
+
*it++ = (void*)(data + i * sizeof(T));
|
76
90
|
|
77
91
|
}
|
78
92
|
|
@@ -80,4 +94,6 @@
|
|
80
94
|
|
81
95
|
}
|
82
96
|
|
97
|
+
|
98
|
+
|
83
99
|
```
|
1
修正
test
CHANGED
@@ -76,7 +76,7 @@
|
|
76
76
|
|
77
77
|
}
|
78
78
|
|
79
|
-
return (NPtr<T, Dim>::type) result;
|
79
|
+
return (typename NPtr<T, Dim>::type) result;
|
80
80
|
|
81
81
|
}
|
82
82
|
|