回答編集履歴

2

m

2021/03/20 01:22

投稿

yumetodo
yumetodo

スコア5850

test CHANGED
@@ -63,3 +63,109 @@
63
63
 
64
64
 
65
65
  `func`の中の`N`が`size_t`というのは何を言ってるのか理解できなかったのでtupleにしときました。
66
+
67
+
68
+
69
+ ---
70
+
71
+
72
+
73
+
74
+
75
+ 追記
76
+
77
+
78
+
79
+ `std::common_type`を使おうとしているということは、完全に同一ではない型でも許容したいということだと思います。すると`Args`を直接`std::common_type`にいれるのはまずくて、要素型を取り出してそれに対して適用しなければならないと思います。
80
+
81
+
82
+
83
+ C++20で追加されたconceptとC++17で追加されたfold式を組み合わせるとこんなふうに書けるかと思います。
84
+
85
+
86
+
87
+ ```cpp
88
+
89
+ #include <cstddef>
90
+
91
+ #include <type_traits>
92
+
93
+ template<typename T,size_t N>
94
+
95
+ struct Boo{};
96
+
97
+ namespace traits {
98
+
99
+ template<typename> struct boo;
100
+
101
+ template<typename T, size_t N>
102
+
103
+ struct boo<Boo<T, N>> : std::integral_constant<std::size_t, N>
104
+
105
+ {
106
+
107
+ using type = T;
108
+
109
+ };
110
+
111
+ template<typename T> using boo_elem_t = typename boo<T>::type;
112
+
113
+
114
+
115
+ template<typename ...Args>
116
+
117
+ concept calculatable_boos = requires (Args ...args) {
118
+
119
+ requires sizeof...(Args) >= 2;
120
+
121
+ typename std::common_type<boo_elem_t<Args>...>::type;
122
+
123
+ { ( boo<Args>::value == ... ) };
124
+
125
+ };
126
+
127
+ }
128
+
129
+ template<typename ...Args>
130
+
131
+ auto func(const Args...)
132
+
133
+ requires traits::calculatable_boos<Args...>
134
+
135
+ {
136
+
137
+
138
+
139
+ }
140
+
141
+
142
+
143
+ int main(){
144
+
145
+ Boo<int,1> a;
146
+
147
+ Boo<int,1> b;
148
+
149
+ Boo<short,1> c;
150
+
151
+ func(a,b);
152
+
153
+ func(a,c);
154
+
155
+ }
156
+
157
+ ```
158
+
159
+
160
+
161
+ [https://wandbox.org/permlink/mDXg3XmIvcB6ybdF](https://wandbox.org/permlink/mDXg3XmIvcB6ybdF)
162
+
163
+
164
+
165
+ ref:
166
+
167
+ - [コンセプト - cpprefjp C++日本語リファレンス](https://cpprefjp.github.io/lang/cpp20/concepts.html)
168
+
169
+ - [畳み込み式 - cpprefjp C++日本語リファレンス](https://cpprefjp.github.io/lang/cpp17/folding_expressions.html)
170
+
171
+ - [C++20 Concepts: Testing constrained functions - Andreas Fertig's Blog](https://andreasfertig.blog/2020/08/cpp20-concepts-testing-constrained-functions/)

1

m

2021/03/20 01:22

投稿

yumetodo
yumetodo

スコア5850

test CHANGED
@@ -36,6 +36,8 @@
36
36
 
37
37
  using T = sprout::types::type_tuple<typename traits::boo<Args>::type...>;
38
38
 
39
+ //static_assert(std::is_same_v<sprout::types::type_tuple<int, int>, T>);
40
+
39
41
  [[maybe_unused]] constexpr auto N = std::tuple{traits::boo<Args>::value...};
40
42
 
41
43
  }
@@ -56,7 +58,7 @@
56
58
 
57
59
 
58
60
 
59
- [https://wandbox.org/permlink/GIdvW7MqwNXpWiuh](https://wandbox.org/permlink/GIdvW7MqwNXpWiuh)
61
+ [https://wandbox.org/permlink/ExZ15cwohvOeLLnX](https://wandbox.org/permlink/ExZ15cwohvOeLLnX)
60
62
 
61
63
 
62
64