回答編集履歴
1
修正案を追加
test
CHANGED
@@ -7,3 +7,65 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
この場合は型か非型の問題ではないです。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
# 修正方法
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
修正する方法は状況によって色々と考えられますが、設計をあまり変えないようにという前提であれば `Foo` の型引数の `T` を無駄に受け取るようなテンプレートを挟めばよいです。 これによって `T` が確定するまで `func` が展開されませんので `func` を呼出すときに `func` の解決 (SFINAE) が試みられるため、 `Foo` 自体の展開には失敗しません。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```cpp
|
22
|
+
|
23
|
+
#include <iostream>
|
24
|
+
|
25
|
+
#include <array>
|
26
|
+
|
27
|
+
#include <type_traits>
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
template<class T>
|
32
|
+
|
33
|
+
class always_true : public std::true_type {
|
34
|
+
|
35
|
+
};
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
template<std::size_t N>
|
40
|
+
|
41
|
+
struct Foo{
|
42
|
+
|
43
|
+
template<typename T, typename std::enable_if<(N==1) && always_true<T>::value, std::nullptr_t>::type =nullptr>
|
44
|
+
|
45
|
+
std::array<T,1>
|
46
|
+
|
47
|
+
func(void){std::array<T,1> a; return a;}
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
template<typename T, typename std::enable_if<(N>1) && always_true<T>::value, std::nullptr_t>::type =nullptr>
|
52
|
+
|
53
|
+
std::array<T,N>
|
54
|
+
|
55
|
+
func(void){std::array<T,N> a; return a;}
|
56
|
+
|
57
|
+
};
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
int main(void)
|
62
|
+
|
63
|
+
{
|
64
|
+
|
65
|
+
Foo<(size_t)1> f;
|
66
|
+
|
67
|
+
std::array<int,1> ar = f.func<int>();
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
```
|