第330章 ポインターはどうなるか?

ポインターとかはどうなるの? って疑問に思う人もいるでしょうから、一応実装例だけ示しておきます。

結論からいうと何も変わらないんですがね…

では実装例です。

  1 #include <type_traits>
  2
  3 template<class T = void>
  4 struct Foo {
  5   Foo(){}
  6   Foo(const Foo&){}
  7 };
  8
  9 template<class T>
 10 struct Bar {
 11   Bar(T,T){}
 12 };
 13
 14 #if __cplusplus >= 201703L
 15 template<class T>
 16 Foo(T) -> Foo<T>;
 17
 18 template<class T = void>
 19 Foo(void) -> Foo<T>;
 20
 21 template<class T>
 22 Bar(T,T) -> Bar<T>;
 23 #endif
 24
 25 int main(){
 26 #if __cplusplus >= 201703L
 27   auto *a = new Foo{};
 28   auto *b = new Bar{1,2};
 29 #else
 30   auto *a = new Foo<void>{};
 31   auto *b = new Bar<int>{1,2};
 32 #endif
 33   static_assert(std::is_same<decltype(a),Foo<void>*>::value,"");
 34   static_assert(std::is_same<decltype(b),Bar<int>*>::value,"");
 35   delete a;
 36   delete b;
 37 }

このように new 演算子でコンストラクターを呼び出しても同じ効果を保持してくれます。

Copyright 2017-2018, by Masaki Komatsu