回答編集履歴
2
appendix
answer
CHANGED
@@ -19,4 +19,19 @@
|
|
19
19
|
Test<int, int> t1;
|
20
20
|
t1.func(100); // T=U=int
|
21
21
|
```
|
22
|
-
[https://wandbox.org/permlink/j5I48l571fJMYtLH](https://wandbox.org/permlink/j5I48l571fJMYtLH)
|
22
|
+
[https://wandbox.org/permlink/j5I48l571fJMYtLH](https://wandbox.org/permlink/j5I48l571fJMYtLH)
|
23
|
+
|
24
|
+
----
|
25
|
+
かなりトリッキーな書き方になってしまいますが、直接的にメンバ関数オーバーロード`Test::func(const T&)`を無効化する方法もあります。(個人的にはクラステンプレート特殊化の方が分かりやすくて好みですね)
|
26
|
+
|
27
|
+
```C++
|
28
|
+
template<typename T, typename U>
|
29
|
+
struct Test {
|
30
|
+
template<bool AlwaysTrue = true>
|
31
|
+
std::enable_if_t<!std::is_same_v<T, U> && AlwaysTrue>
|
32
|
+
func(const T&){ std::puts("T"); }
|
33
|
+
|
34
|
+
void func(const U&){ std::puts("U"); }
|
35
|
+
};
|
36
|
+
```
|
37
|
+
[https://wandbox.org/permlink/f1OcI1x9Tq9hLaIq](https://wandbox.org/permlink/f1OcI1x9Tq9hLaIq)
|
1
fix comment
answer
CHANGED
@@ -17,6 +17,6 @@
|
|
17
17
|
t0.func(3.14); // U=double
|
18
18
|
|
19
19
|
Test<int, int> t1;
|
20
|
-
t1.func(100); // T=U=
|
20
|
+
t1.func(100); // T=U=int
|
21
21
|
```
|
22
22
|
[https://wandbox.org/permlink/j5I48l571fJMYtLH](https://wandbox.org/permlink/j5I48l571fJMYtLH)
|