質問編集履歴

1

文章を修正してCodeを追加しました。

2020/08/31 05:55

投稿

Y.R.T
Y.R.T

スコア42

test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,431 @@
11
11
 
12
12
 
13
13
  このメッセセージが出てデバックが出来ないです。
14
+
15
+
16
+
17
+ <修正>
18
+
19
+ すみません、ステップインデバックはできるようです。
20
+
21
+ しかし、上記メッセージは何でしょうか?
22
+
23
+ 実際に実行しても何も実行結果が表示さずに終了して
24
+
25
+ しまいます。
26
+
27
+
28
+
29
+ ```
30
+
31
+ #include <cmath>
32
+
33
+ #include <assert.h>
34
+
35
+
36
+
37
+ bool are_equal(double const d1, double const d2, double const epsilon = 0.001)
38
+
39
+ {
40
+
41
+ return std::fabs(d1 - d2) < epsilon;
42
+
43
+ }
44
+
45
+
46
+
47
+ namespace temperature
48
+
49
+ {
50
+
51
+ enum class scale
52
+
53
+ {
54
+
55
+ celsius,
56
+
57
+ fahrenheit,
58
+
59
+ kelvin
60
+
61
+ };
62
+
63
+
64
+
65
+ template <scale S>
66
+
67
+ class quantity
68
+
69
+ {
70
+
71
+ const double amount;
72
+
73
+ public:
74
+
75
+ constexpr explicit quantity(double const a) : amount(a) {}
76
+
77
+
78
+
79
+ explicit operator double() const { return amount; }
80
+
81
+ };
82
+
83
+
84
+
85
+ template <scale S>
86
+
87
+ inline bool operator==(quantity<S> const& lhs, quantity<S> const& rhs)
88
+
89
+ {
90
+
91
+ return are_equal(static_cast<double>(lhs), static_cast<double>(rhs));
92
+
93
+ }
94
+
95
+
96
+
97
+ template <scale S>
98
+
99
+ inline bool operator!=(quantity<S> const& lhs, quantity<S> const& rhs)
100
+
101
+ {
102
+
103
+ return !(lhs == rhs);
104
+
105
+ }
106
+
107
+
108
+
109
+ template <scale S>
110
+
111
+ inline bool operator< (quantity<S> const& lhs, quantity<S> const& rhs)
112
+
113
+ {
114
+
115
+ return static_cast<double>(lhs) < static_cast<double>(rhs);
116
+
117
+ }
118
+
119
+
120
+
121
+ template <scale S>
122
+
123
+ inline bool operator> (quantity<S> const& lhs, quantity<S> const& rhs)
124
+
125
+ {
126
+
127
+ return rhs < lhs;
128
+
129
+ }
130
+
131
+
132
+
133
+ template <scale S>
134
+
135
+ inline bool operator<=(quantity<S> const& lhs, quantity<S> const& rhs)
136
+
137
+ {
138
+
139
+ return !(lhs > rhs);
140
+
141
+ }
142
+
143
+
144
+
145
+ template <scale S>
146
+
147
+ inline bool operator>=(quantity<S> const& lhs, quantity<S> const& rhs)
148
+
149
+ {
150
+
151
+ return !(lhs < rhs);
152
+
153
+ }
154
+
155
+
156
+
157
+ template <scale S>
158
+
159
+ constexpr quantity<S> operator+(quantity<S> const& q1, quantity<S> const& q2)
160
+
161
+ {
162
+
163
+ return quantity<S>(static_cast<double>(q1) + static_cast<double>(q2));
164
+
165
+ }
166
+
167
+
168
+
169
+ template <scale S>
170
+
171
+ constexpr quantity<S> operator-(quantity<S> const& q1, quantity<S> const& q2)
172
+
173
+ {
174
+
175
+ return quantity<S>(static_cast<double>(q1) - static_cast<double>(q2));
176
+
177
+ }
178
+
179
+
180
+
181
+ template <scale S, scale R>
182
+
183
+ struct conversion_traits
184
+
185
+ {
186
+
187
+ static double convert(double const value) = delete;
188
+
189
+ };
190
+
191
+
192
+
193
+ template <>
194
+
195
+ struct conversion_traits<scale::celsius, scale::kelvin>
196
+
197
+ {
198
+
199
+ static double convert(double const value)
200
+
201
+ {
202
+
203
+ return value + 273.15;
204
+
205
+ }
206
+
207
+ };
208
+
209
+
210
+
211
+ template <>
212
+
213
+ struct conversion_traits<scale::kelvin, scale::celsius>
214
+
215
+ {
216
+
217
+ static double convert(double const value)
218
+
219
+ {
220
+
221
+ return value - 273.15;
222
+
223
+ }
224
+
225
+ };
226
+
227
+
228
+
229
+ template <>
230
+
231
+ struct conversion_traits<scale::celsius, scale::fahrenheit>
232
+
233
+ {
234
+
235
+ static double convert(double const value)
236
+
237
+ {
238
+
239
+ return (value * 9) / 5 + 32;;
240
+
241
+ }
242
+
243
+ };
244
+
245
+
246
+
247
+ template <>
248
+
249
+ struct conversion_traits<scale::fahrenheit, scale::celsius>
250
+
251
+ {
252
+
253
+ static double convert(double const value)
254
+
255
+ {
256
+
257
+ return (value - 32) * 5 / 9;
258
+
259
+ }
260
+
261
+ };
262
+
263
+
264
+
265
+ template <>
266
+
267
+ struct conversion_traits<scale::fahrenheit, scale::kelvin>
268
+
269
+ {
270
+
271
+ static double convert(double const value)
272
+
273
+ {
274
+
275
+ return (value + 459.67) * 5 / 9;
276
+
277
+ }
278
+
279
+ };
280
+
281
+
282
+
283
+ template <>
284
+
285
+ struct conversion_traits<scale::kelvin, scale::fahrenheit>
286
+
287
+ {
288
+
289
+ static double convert(double const value)
290
+
291
+ {
292
+
293
+ return (value * 9) / 5 - 459.67;
294
+
295
+ }
296
+
297
+ };
298
+
299
+
300
+
301
+ template <scale R, scale S>
302
+
303
+ constexpr quantity<R> temperature_cast(quantity<S> const q)
304
+
305
+ {
306
+
307
+ return quantity<R>(conversion_traits<S, R>::convert(static_cast<double>(q)));
308
+
309
+ }
310
+
311
+
312
+
313
+ namespace temperature_scale_literals
314
+
315
+ {
316
+
317
+ constexpr quantity<scale::celsius> operator "" _deg(long double const amount)
318
+
319
+ {
320
+
321
+ return quantity<scale::celsius> {static_cast<double>(amount)};
322
+
323
+ }
324
+
325
+
326
+
327
+ constexpr quantity<scale::fahrenheit> operator "" _f(long double const amount)
328
+
329
+ {
330
+
331
+ return quantity<scale::fahrenheit> {static_cast<double>(amount)};
332
+
333
+ }
334
+
335
+
336
+
337
+ constexpr quantity<scale::kelvin> operator "" _k(long double const amount)
338
+
339
+ {
340
+
341
+ return quantity<scale::kelvin> {static_cast<double>(amount)};
342
+
343
+ }
344
+
345
+ }
346
+
347
+ }
348
+
349
+
350
+
351
+ int main()
352
+
353
+ {
354
+
355
+ using namespace temperature;
356
+
357
+ using namespace temperature_scale_literals;
358
+
359
+
360
+
361
+ auto t1{ 36.5_deg };
362
+
363
+ auto t2{ 79.0_f };
364
+
365
+ auto t3{ 100.0_k };
366
+
367
+
368
+
369
+ {
370
+
371
+ auto tf = temperature_cast<scale::fahrenheit>(t1);
372
+
373
+ auto tc = temperature_cast<scale::celsius>(tf);
374
+
375
+ assert(t1 == tc);
376
+
377
+ }
378
+
379
+
380
+
381
+ {
382
+
383
+ auto tk = temperature_cast<scale::kelvin>(t1);
384
+
385
+ auto tc = temperature_cast<scale::celsius>(tk);
386
+
387
+ assert(t1 == tc);
388
+
389
+ }
390
+
391
+
392
+
393
+ {
394
+
395
+ auto tc = temperature_cast<scale::celsius>(t2);
396
+
397
+ auto tf = temperature_cast<scale::fahrenheit>(tc);
398
+
399
+ assert(t2 == tf);
400
+
401
+ }
402
+
403
+
404
+
405
+ {
406
+
407
+ auto tk = temperature_cast<scale::kelvin>(t2);
408
+
409
+ auto tf = temperature_cast<scale::fahrenheit>(tk);
410
+
411
+ assert(t2 == tf);
412
+
413
+ }
414
+
415
+
416
+
417
+ {
418
+
419
+ auto tc = temperature_cast<scale::celsius>(t3);
420
+
421
+ auto tk = temperature_cast<scale::kelvin>(tc);
422
+
423
+ assert(t3 == tk);
424
+
425
+ }
426
+
427
+
428
+
429
+ {
430
+
431
+ auto tf = temperature_cast<scale::fahrenheit>(t3);
432
+
433
+ auto tk = temperature_cast<scale::kelvin>(tf);
434
+
435
+ assert(t3 == tk);
436
+
437
+ }
438
+
439
+ }
440
+
441
+ ```