Skip to content

Instantly share code, notes, and snippets.

@plasma-effect
Created November 27, 2016 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plasma-effect/389ed5745ec582229c91fe7d83551a4b to your computer and use it in GitHub Desktop.
Save plasma-effect/389ed5745ec582229c91fe7d83551a4b to your computer and use it in GitHub Desktop.
light_adt

plasma.ADTにlight_adt.hppを追加した。使い方は以下の通り。

#include<iostream>
#include<string>
#include"light_adt.hpp"

using namespace plasma_adt;
struct list :data_type<list, int, tuple<int, list>>
{
	template<class T>list(T&& arg) :data_type(std::forward<T>(arg))
	{

	}
};
const auto Tale = list::get_instance<0>();
const auto List = list::get_instance<1>();
void print(list const& lis)
{
	if (auto p = lis.get(List))
	{
		auto const& q = p->get();
		std::cout << q.get<0>() << ",";
		print(q.get<1>());
	}
	else if (auto p = lis.get(Tale))
	{
		auto const& q = p->get();
		std::cout << q << std::endl;
	}
}

int main()
{
	auto test = List(1, List(2, List(3, Tale(4))));
	print(test);
}

最大の特徴はパターンマッチを廃止したこと。
templateメンバ関数getを使ってどの型かをチェックする。
getの返り値はboost::optional<std::reference_wrapper<const type>>(VS2017RCで使う場合のみstd::optional)
よって上のように一旦auto const& val = opt->get()で受けると使いやすい。 #注意 Visual Studio 2017 RCとclang 4.0.0とgcc 7.0.0で動作確認している。Visual Studio 2015はつらみがつらすぎるので知らん。
あとtupleの中身は8個以下でないといけない(9個も10個も入れる人はいないだろうが)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment