LoginSignup
5
1

More than 5 years have passed since last update.

Go2ジェネリクスとC++20コンセプトって雰囲気ちょい似てる?

Last updated at Posted at 2019-02-20

Go2のジェネリクス/コントラクトと、C++20のコンセプトって雰囲気が似てるんじゃないかと思って対比させてみただけの記事。(※個人の感想です)

// Go2
contract stringer(x T) {
  var s string = x.String()
}

func Stringify(type T stringer)(s []T) (ret []string) {
  for _, v := range s {
    ret = append(ret, v.String())
  }
  return ret
}
// C++20
#include <string>
#include <span>
#include <vector>
using namespace std;

template <typename T>
concept Stringer = requires (T x) {
  { x.to_string() } -> string;
};

template <Stringer E>
auto stringify(span<E> s) -> vector<string>
{
  vector<string> ret;
  for (auto&& v: s) {
     ret.push_back(v.to_string());
  }
  return ret;
}

いかがでしたか?いくらなんでも無理がありますね。

免責事項

2019年2月現在、Go2もC++20も策定段階のプログラミング言語仕様です。最終的に本記事の内容は陳腐化する可能性があります。

Go2ジェネリクス仕様は Contracts - Draft Design, Aug/27/2018 を参考にしました。
C++20コンセプト仕様は N4800 Working Draft 仕様に基づきます。

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1