teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

getを工夫

2016/05/13 16:40

投稿

yumetodo
yumetodo

スコア5852

answer CHANGED
@@ -3,6 +3,7 @@
3
3
  ```
4
4
  #include <iostream>
5
5
  #include <cstdint>
6
+ #include <type_traits>
6
7
  class Direction {
7
8
  public:
8
9
  using value_type = std::uint8_t;
@@ -30,18 +31,21 @@
30
31
  constexpr void reverse() noexcept {
31
32
  this->value = (this->value + 2) & 0b11;
32
33
  }
34
+ template<typename T = value_type, std::enable_if_t<std::is_arithmetic<T>::value, std::nullptr_t> = nullptr>
33
- constexpr value_type get() const noexcept {
35
+ constexpr T get() const noexcept {
34
36
  return this->value;
35
37
  }
36
38
  };
37
39
 
38
40
  int main() {
39
41
  Direction a(Direction::front);
40
- std::cout << a.get() << std::endl;
42
+ std::cout << a.get<unsigned>() << std::endl;
41
- a.turnLeft();
43
+ a.turn_left();
42
- std::cout << a.get() << std::endl;
44
+ std::cout << a.get<unsigned>() << std::endl;
43
45
  return 0;
44
46
  }
45
47
  ```
46
48
 
49
+ こんなのかな。
50
+ [動作](http://melpon.org/wandbox/permlink/MlttJTjNsdm9lv94)
47
- こんなのかな。sprout使えばコンパイル時に文字列使えるけどそこまでしたくない
51
+ sprout使えばコンパイル時に文字列使えるけどそこまでしたくない