回答編集履歴

2

appendix

2018/11/10 13:41

投稿

yohhoy
yohhoy

スコア6189

test CHANGED
@@ -41,3 +41,33 @@
41
41
  ```
42
42
 
43
43
  [https://wandbox.org/permlink/j5I48l571fJMYtLH](https://wandbox.org/permlink/j5I48l571fJMYtLH)
44
+
45
+
46
+
47
+ ----
48
+
49
+ かなりトリッキーな書き方になってしまいますが、直接的にメンバ関数オーバーロード`Test::func(const T&)`を無効化する方法もあります。(個人的にはクラステンプレート特殊化の方が分かりやすくて好みですね)
50
+
51
+
52
+
53
+ ```C++
54
+
55
+ template<typename T, typename U>
56
+
57
+ struct Test {
58
+
59
+ template<bool AlwaysTrue = true>
60
+
61
+ std::enable_if_t<!std::is_same_v<T, U> && AlwaysTrue>
62
+
63
+ func(const T&){ std::puts("T"); }
64
+
65
+
66
+
67
+ void func(const U&){ std::puts("U"); }
68
+
69
+ };
70
+
71
+ ```
72
+
73
+ [https://wandbox.org/permlink/f1OcI1x9Tq9hLaIq](https://wandbox.org/permlink/f1OcI1x9Tq9hLaIq)

1

fix comment

2018/11/10 13:40

投稿

yohhoy
yohhoy

スコア6189

test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  Test<int, int> t1;
38
38
 
39
- t1.func(100); // T=U=double
39
+ t1.func(100); // T=U=int
40
40
 
41
41
  ```
42
42