回答編集履歴
3
update
answer
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
> [...] For another thing, function template __specializations don't overload.__ This means that any specializations you write will not affect which template gets used, which runs counter to what most people would intuitively expect. After all, if you had written a nontemplate function with the identical signature instead of a function template specialization, the nontemplate function would always be selected because it's always considered to be a better match than a template. [...]
|
12
12
|
|
13
13
|
---
|
14
|
-
記事中で
|
14
|
+
記事中で言及されているコード例:
|
15
15
|
|
16
16
|
```C++
|
17
17
|
template<class T> // (a) (プライマリ)テンプレート
|
@@ -41,4 +41,6 @@
|
|
41
41
|
|
42
42
|
int *p = nullptr;
|
43
43
|
f( p ); // "b"を出力
|
44
|
-
```
|
44
|
+
```
|
45
|
+
|
46
|
+
詳細は記事に譲りますが、関数の解決プロセスではテンプレート関数の完全特殊化は無視され、(プライマリテンプレートの)関数オーバーロードのみが考慮されるため、このような結果となるようです。
|
2
refine
answer
CHANGED
@@ -14,29 +14,29 @@
|
|
14
14
|
記事中で触れられている例:
|
15
15
|
|
16
16
|
```C++
|
17
|
-
template<class T>
|
17
|
+
template<class T> // (a) (プライマリ)テンプレート
|
18
18
|
void f( T ) { std::puts("a"); }
|
19
19
|
|
20
|
-
template<class T>
|
20
|
+
template<class T> // (b) 別のプライマリテンプレート, (a)のオーバーロード
|
21
21
|
void f( T* ) { std::puts("b"); }
|
22
22
|
|
23
|
-
template<>
|
23
|
+
template<> // (c) テンプレート(b)の完全特殊化
|
24
24
|
void f<>(int*) { std::puts("c"); }
|
25
25
|
|
26
26
|
int *p = nullptr;
|
27
|
-
f( p ); // "c"を出力
|
27
|
+
f( p ); // "c"を出力
|
28
28
|
```
|
29
29
|
|
30
30
|
b, c の定義順を入れ替えると...
|
31
31
|
|
32
32
|
```C++
|
33
|
-
template<class T>
|
33
|
+
template<class T> // (a) (プライマリ)テンプレート
|
34
34
|
void f( T ) { std::puts("a"); }
|
35
35
|
|
36
|
-
template<>
|
36
|
+
template<> // (c) テンプレート(a)の完全特殊化
|
37
37
|
void f<>(int*) { std::puts("c"); }
|
38
38
|
|
39
|
-
template<class T>
|
39
|
+
template<class T> // (b) 別のプライマリテンプレート, (a)のオーバーロード
|
40
40
|
void f( T* ) { std::puts("b"); }
|
41
41
|
|
42
42
|
int *p = nullptr;
|
1
update
answer
CHANGED
@@ -8,4 +8,37 @@
|
|
8
8
|
|
9
9
|
Herb Sutter氏記事 "[Why Not Specialize Function Templates?](http://www.gotw.ca/publications/mill17.htm)"
|
10
10
|
> Summary
|
11
|
-
> [...] For another thing, function template __specializations don't overload.__ This means that any specializations you write will not affect which template gets used, which runs counter to what most people would intuitively expect. After all, if you had written a nontemplate function with the identical signature instead of a function template specialization, the nontemplate function would always be selected because it's always considered to be a better match than a template. [...]
|
11
|
+
> [...] For another thing, function template __specializations don't overload.__ This means that any specializations you write will not affect which template gets used, which runs counter to what most people would intuitively expect. After all, if you had written a nontemplate function with the identical signature instead of a function template specialization, the nontemplate function would always be selected because it's always considered to be a better match than a template. [...]
|
12
|
+
|
13
|
+
---
|
14
|
+
記事中で触れられている例:
|
15
|
+
|
16
|
+
```C++
|
17
|
+
template<class T>
|
18
|
+
void f( T ) { std::puts("a"); }
|
19
|
+
|
20
|
+
template<class T>
|
21
|
+
void f( T* ) { std::puts("b"); }
|
22
|
+
|
23
|
+
template<>
|
24
|
+
void f<>(int*) { std::puts("c"); }
|
25
|
+
|
26
|
+
int *p = nullptr;
|
27
|
+
f( p ); // "c"を出力**
|
28
|
+
```
|
29
|
+
|
30
|
+
b, c の定義順を入れ替えると...
|
31
|
+
|
32
|
+
```C++
|
33
|
+
template<class T>
|
34
|
+
void f( T ) { std::puts("a"); }
|
35
|
+
|
36
|
+
template<>
|
37
|
+
void f<>(int*) { std::puts("c"); }
|
38
|
+
|
39
|
+
template<class T>
|
40
|
+
void f( T* ) { std::puts("b"); }
|
41
|
+
|
42
|
+
int *p = nullptr;
|
43
|
+
f( p ); // "b"を出力
|
44
|
+
```
|