SlideShare a Scribd company logo
1 of 27
Download to read offline
Tickライブラリで遊ぼう
北陸 Kernel/VM 探検隊 #1
自己紹介
2
@kotojpn
かわいい
Tickについて
• C++のコンセプトという機能をプリプロセッサ
やテンプレートを用いて実現している!
• http://ericniebler.com/

ここの人が提案したコンセプトチェックの方法
を基にしたライブラリ!
• pfultz2という人が作った!
ここ https://github.com/pfultz2/Tick
3
補足: コンセプトとは
• ジェネリックな型が満たすべき要件定義のこと!
e.g. !
型 T は Forward Iterator のコンセプトを満たす!
!
• C++11の時点で提案はされていたもののずいぶ
ん揉めた結果入らなかった!
• C++17で入ればいいなあ
4
Tick解説
面白そうなのでTickについて調べてみたよ
5
Tick解説
• 作者の github に書かれている例!
!
!
インクリメント可能か調べる特性メタ関数!
is_incrementable<T>!
!
次のように書ける
6
Tick解説
Tickを使った is_incrementable<T> の定義!
7
#include <tick/requires.h>
#include <tick/trait_check.h>
!
TICK_TRAIT(is_incrementable)
{
template <class T>
auto requires_(T&& x) -> TICK_VALID(
++x,
x++
);
};
Tick解説
マクロを展開してみる!
8
!
struct tick_private_trait_base_is_incrementable
: tick::ops {
typedef tick::refines<> type;
};
struct tick_private_trait_is_incrementable;
template<class... T>
struct is_incrementable :
tick::models<tick_private_trait_is_incrementable(T...)> {};
!
struct tick_private_trait_is_incrementable :
tick::detail::base_requires, tick::ops,
tick_private_trait_base_is_incrementable::type
{
template<class T>
auto requires_(T&& x)
-> decltype(tick::detail::valid_expr((x++, ++x,
tick::detail::void_())));
};
Tick解説
わかりにくいので書き換え!
!
!
!
!
!
!
!
9
template<class... Ts>
struct holder { using type = void; };
!
template <class Trait, class X = void>
struct models : false_type {};
!
template <class Trait, class T>
struct models<Trait(T),
typename holder<
decltype(declval<Trait>().requires_(declval<T>()))
>::type>
: true_type {};
!
struct is_incrementable_impl
{
template <class T>
auto requires_(T&& x) -> decltype(x++, ++x);
};
!
template <class T>
struct is_incrementable : models<is_incrementable_impl(T)> {};
Tick解説
わかりやすくなったのか???!
!
• とりあえず太線にしたところが重要
10
Tick解説(削除ページ)
補足: この場合はこう書いたほうが楽!
8
!
struct is_incrementable_impl {
template <class T>
static auto check(T*) -> decltype(
std::declval<T&>()++, ++std::declval<T&>(),
std::true_type());
!
static auto check(...) -> std::false_type;
};
!
template <class T>
struct is_incrementable
: decltype(is_incrementable_impl::check<T>(nullptr))
{};
Tick解説
11
一方, これを利用するを使う関数は
Tick解説
• is_incrementable<T> を使った関数!
!
!
!
!
!
!
!
TICK_REQUIRESマクロに特性メタ関数をいれればOK
12
!
template <
class T,
TICK_REQUIRES(is_incrementable<T>::value)
>
void increment(T& x)
{
x++;
}
Tick解説
• これもマクロ展開してみる!
!
!
!
!
!
!
!
enable_if 使ってるだけ
13
!
Template<
class T,
typename
std::enable_if<is_incrementable<T>::value,
int>::type = 0)
>
void increment(T& x)
{
x++;
}
Tick解説
もう少し複雑なものをも簡潔に書けるよ!
!
14
Tick解説
• 多少複雑な例 ( has_hoge )!
!
has_hoge<T> の要件!
• 型 T がメンバ関数 hoge() をもっている!
• その返り値が int型である
15
Tick解説
has_hoge<T> の例
16
#include <tick/requires.h>
#include <tick/trait_check.h>
!
TICK_TRAIT(has_hoge)
{
template <class T>
auto requires_(T&& x) -> TICK_VALID(
returns<int>(x.hoge())
);
};
Tick解説
• 他にも様々な機能があります!
• 作者のgithubのREADMEがかなり充実している!
• なので, そちらを参考にしたほうが早い...
17
Tickで遊ぶ
• 実際に型制約を Tick で書き換えてみたよ!
!
ここの point_traits の型制約!
https://gist.github.com/kotoji/af5485cef6e98466328e
18
Tickで遊ぶ
• 実際に型制約を Tick で書き換えてみたよ!
!
!
!
こんな感じ
19
Tickで遊ぶ
point_trait<T> の型制約
20
namespace util { namespace detail {
namespace mpl = boost::mpl;
!template <typename Cond, typename Seq, std::size_t I>
struct any_impl {
static constexpr bool value = mpl::apply<Cond,
typename std::tuple_element<I, Seq>::type
>::type::value &&
any_impl<Cond, Seq, I - 1>::value;
};
template <typename Cond, typename Seq>
struct any_impl<Cond, Seq, 0> {
static constexpr bool value = mpl::apply<Cond,
typename std::tuple_element<0, Seq>::type
>::type::value;
};
} // namespace detail
template <typename Cond, typename... Types>
struct any_ : detail::any_impl<Cond, std::tuple<Types...> , sizeof...(Types) - 1> {};
} // namespace util
template <typename T,
typename U = decltype(std::declval<T>().x),
typename U1 = decltype(std::declval<T>().y),
std::enable_if<util::any_<std::is_floating_point<boost::mpl::_1> ,U, U1>::value &&
std::is_default_constructible<T>::value
>::type* = nullptr
>
struct point_traits {
// 実装略
};
Tickで遊ぶ
うわあ
21
Tickで遊ぶ
一応説明!
point_traits<T> の型制約!
• T は実数型のメンバ変数 x を持つ!
• T は実数型のメンバ変数 y を持つ!
• T は default constructible である!
• それ以外の場合は自分でアダプトしてね
22
Tickで遊ぶ
Tickで新しくなった point_traits
23
TICK_TRAIT(has_x)
{
template <class T>
auto requires_(T&& p) -> TICK_VALID(
returns<std::is_floating_point<_>>(p.x)
);
};
!
TICK_TRAIT(has_y)
{
template <class T>
auto requires_(T&& p) -> TICK_VALID(
returns<std::is_floating_point<_>>(p.y)
);
};
template <class T,
TICK_REQUIRES(has_x<T>() and has_y<T>() and
std::is_default_constructible<T>::value)
>
struct point_traits {
// 実装略
};
Tickで遊ぶ
もともとのコードがアレだったとはいえ!
Tick を使って簡潔に書くことが出来た!
ウレシイヤッター
24
Tickまとめ
• 特性メタ関数が割りと簡単に書ける!
• TICK_REQUIRES マクロに特性メタ関数を並
べるだけで色々な制約を組み合わせ可能!
!
25
Tickまとめ
色々な型制約を組み合わせて!
君だけのコンセプトをつくろう
26

More Related Content

What's hot

Polyphony: Python ではじめる FPGA
Polyphony: Python ではじめる FPGAPolyphony: Python ではじめる FPGA
Polyphony: Python ではじめる FPGAryos36
 
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.kiki utagawa
 
リテラル文字列型までの道
リテラル文字列型までの道リテラル文字列型までの道
リテラル文字列型までの道Satoshi Sato
 
すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!Genya Murakami
 
Python physicalcomputing
Python physicalcomputingPython physicalcomputing
Python physicalcomputingNoboru Irieda
 
Polyphony の並列化
Polyphony の並列化Polyphony の並列化
Polyphony の並列化ryos36
 
Python と型アノテーション
Python と型アノテーションPython と型アノテーション
Python と型アノテーションK Yamaguchi
 
Polyphony IO まとめ
Polyphony IO まとめPolyphony IO まとめ
Polyphony IO まとめryos36
 
Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)Tetsuya Morimoto
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cythonAtsuo Ishimoto
 
5分でわかるGoのポインタ
5分でわかるGoのポインタ5分でわかるGoのポインタ
5分でわかるGoのポインタY N
 
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...Yoshifumi Kawai
 
Mono is Dead
Mono is DeadMono is Dead
Mono is Deadmelpon
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cythonfuzzysphere
 
Pythonと型チェッカー
Pythonと型チェッカーPythonと型チェッカー
Pythonと型チェッカーTetsuya Morimoto
 
One - Common Lispでもワンライナーしたい
One - Common LispでもワンライナーしたいOne - Common Lispでもワンライナーしたい
One - Common Lispでもワンライナーしたいt-sin
 
Cython ことはじめ
Cython ことはじめCython ことはじめ
Cython ことはじめgion_XY
 

What's hot (20)

Qt小技(修正版)
Qt小技(修正版)Qt小技(修正版)
Qt小技(修正版)
 
Polyphony: Python ではじめる FPGA
Polyphony: Python ではじめる FPGAPolyphony: Python ではじめる FPGA
Polyphony: Python ではじめる FPGA
 
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
 
リテラル文字列型までの道
リテラル文字列型までの道リテラル文字列型までの道
リテラル文字列型までの道
 
すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!
 
Python physicalcomputing
Python physicalcomputingPython physicalcomputing
Python physicalcomputing
 
Polyphony の並列化
Polyphony の並列化Polyphony の並列化
Polyphony の並列化
 
Python と型アノテーション
Python と型アノテーションPython と型アノテーション
Python と型アノテーション
 
Q planet
Q planetQ planet
Q planet
 
Polyphony IO まとめ
Polyphony IO まとめPolyphony IO まとめ
Polyphony IO まとめ
 
Inside FastEnum
Inside FastEnumInside FastEnum
Inside FastEnum
 
Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
5分でわかるGoのポインタ
5分でわかるGoのポインタ5分でわかるGoのポインタ
5分でわかるGoのポインタ
 
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
 
Mono is Dead
Mono is DeadMono is Dead
Mono is Dead
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
 
Pythonと型チェッカー
Pythonと型チェッカーPythonと型チェッカー
Pythonと型チェッカー
 
One - Common Lispでもワンライナーしたい
One - Common LispでもワンライナーしたいOne - Common Lispでもワンライナーしたい
One - Common Lispでもワンライナーしたい
 
Cython ことはじめ
Cython ことはじめCython ことはじめ
Cython ことはじめ
 

Viewers also liked

Ticks identification
Ticks identificationTicks identification
Ticks identificationOsama Zahid
 
Container Monitoring with Sysdig
Container Monitoring with SysdigContainer Monitoring with Sysdig
Container Monitoring with SysdigSreenivas Makam
 
Monitoring docker container and dockerized applications
Monitoring docker container and dockerized applicationsMonitoring docker container and dockerized applications
Monitoring docker container and dockerized applicationsAnanth Padmanabhan
 
Time Series Database and Tick Stack
Time Series Database and Tick StackTime Series Database and Tick Stack
Time Series Database and Tick StackGianluca Arbezzano
 
Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)
Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)
Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)Seungmin Yu
 
Tick and Disease caused by them.
Tick and Disease caused by them.Tick and Disease caused by them.
Tick and Disease caused by them.Raaz Eve Mishra
 
Comprehensive Monitoring for Docker
Comprehensive Monitoring for DockerComprehensive Monitoring for Docker
Comprehensive Monitoring for DockerChristian Beedgen
 
Running High Performance and Fault Tolerant Elasticsearch Clusters on Docker
Running High Performance and Fault Tolerant Elasticsearch Clusters on DockerRunning High Performance and Fault Tolerant Elasticsearch Clusters on Docker
Running High Performance and Fault Tolerant Elasticsearch Clusters on DockerSematext Group, Inc.
 
Internet of Things: What is it? What makes it Tick? What you need to know.
Internet of Things: What is it? What makes it Tick? What you need to know.Internet of Things: What is it? What makes it Tick? What you need to know.
Internet of Things: What is it? What makes it Tick? What you need to know.Extreme Networks
 
A Gentle Introduction To Docker And All Things Containers
A Gentle Introduction To Docker And All Things ContainersA Gentle Introduction To Docker And All Things Containers
A Gentle Introduction To Docker And All Things ContainersJérôme Petazzoni
 
Docker Tips And Tricks at the Docker Beijing Meetup
Docker Tips And Tricks at the Docker Beijing MeetupDocker Tips And Tricks at the Docker Beijing Meetup
Docker Tips And Tricks at the Docker Beijing MeetupJérôme Petazzoni
 

Viewers also liked (11)

Ticks identification
Ticks identificationTicks identification
Ticks identification
 
Container Monitoring with Sysdig
Container Monitoring with SysdigContainer Monitoring with Sysdig
Container Monitoring with Sysdig
 
Monitoring docker container and dockerized applications
Monitoring docker container and dockerized applicationsMonitoring docker container and dockerized applications
Monitoring docker container and dockerized applications
 
Time Series Database and Tick Stack
Time Series Database and Tick StackTime Series Database and Tick Stack
Time Series Database and Tick Stack
 
Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)
Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)
Custom DevOps Monitoring System in MelOn (with InfluxDB + Telegraf + Grafana)
 
Tick and Disease caused by them.
Tick and Disease caused by them.Tick and Disease caused by them.
Tick and Disease caused by them.
 
Comprehensive Monitoring for Docker
Comprehensive Monitoring for DockerComprehensive Monitoring for Docker
Comprehensive Monitoring for Docker
 
Running High Performance and Fault Tolerant Elasticsearch Clusters on Docker
Running High Performance and Fault Tolerant Elasticsearch Clusters on DockerRunning High Performance and Fault Tolerant Elasticsearch Clusters on Docker
Running High Performance and Fault Tolerant Elasticsearch Clusters on Docker
 
Internet of Things: What is it? What makes it Tick? What you need to know.
Internet of Things: What is it? What makes it Tick? What you need to know.Internet of Things: What is it? What makes it Tick? What you need to know.
Internet of Things: What is it? What makes it Tick? What you need to know.
 
A Gentle Introduction To Docker And All Things Containers
A Gentle Introduction To Docker And All Things ContainersA Gentle Introduction To Docker And All Things Containers
A Gentle Introduction To Docker And All Things Containers
 
Docker Tips And Tricks at the Docker Beijing Meetup
Docker Tips And Tricks at the Docker Beijing MeetupDocker Tips And Tricks at the Docker Beijing Meetup
Docker Tips And Tricks at the Docker Beijing Meetup
 

Similar to Tickライブラリで遊ぼう(C++)

C# 7.2 with .NET Core 2.1
C# 7.2 with .NET Core 2.1C# 7.2 with .NET Core 2.1
C# 7.2 with .NET Core 2.1信之 岩永
 
C++コミュニティーの中心でC++をDISる
C++コミュニティーの中心でC++をDISるC++コミュニティーの中心でC++をDISる
C++コミュニティーの中心でC++をDISるHideyuki Tanaka
 
わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」
わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」
わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」Takeshi Kiriya
 
かわいくなろうとしたら語彙力が下がった話
かわいくなろうとしたら語彙力が下がった話かわいくなろうとしたら語彙力が下がった話
かわいくなろうとしたら語彙力が下がった話京大 マイコンクラブ
 
C#言語機能の作り方
C#言語機能の作り方C#言語機能の作り方
C#言語機能の作り方信之 岩永
 
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~Kei IWASAKI
 
gitを使って、レポジトリの一部抽出forkしてみました
gitを使って、レポジトリの一部抽出forkしてみましたgitを使って、レポジトリの一部抽出forkしてみました
gitを使って、レポジトリの一部抽出forkしてみましたTakako Miyagawa
 
競技プログラミングのためのC++入門
競技プログラミングのためのC++入門競技プログラミングのためのC++入門
競技プログラミングのためのC++入門natrium11321
 
中3女子でもわかる constexpr
中3女子でもわかる constexpr中3女子でもわかる constexpr
中3女子でもわかる constexprGenya Murakami
 
C#や.NET Frameworkがやっていること
C#や.NET FrameworkがやっていることC#や.NET Frameworkがやっていること
C#や.NET Frameworkがやっていること信之 岩永
 
Kotlinでマッチョする話
Kotlinでマッチョする話Kotlinでマッチョする話
Kotlinでマッチョする話Shinobu Okano
 
js-ctypes - ネイティブコードを呼び出す新しいカタチ
js-ctypes - ネイティブコードを呼び出す新しいカタチjs-ctypes - ネイティブコードを呼び出す新しいカタチ
js-ctypes - ネイティブコードを呼び出す新しいカタチMakoto Kato
 
PHP7をDockerで動かしたという話
PHP7をDockerで動かしたという話PHP7をDockerで動かしたという話
PHP7をDockerで動かしたという話侑弥 濱田
 
中3女子が狂える本当に気持ちのいい constexpr
中3女子が狂える本当に気持ちのいい constexpr中3女子が狂える本当に気持ちのいい constexpr
中3女子が狂える本当に気持ちのいい constexprGenya Murakami
 

Similar to Tickライブラリで遊ぼう(C++) (20)

C# 7.2 with .NET Core 2.1
C# 7.2 with .NET Core 2.1C# 7.2 with .NET Core 2.1
C# 7.2 with .NET Core 2.1
 
C++コミュニティーの中心でC++をDISる
C++コミュニティーの中心でC++をDISるC++コミュニティーの中心でC++をDISる
C++コミュニティーの中心でC++をDISる
 
わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」
わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」
わんくま東京#38 LT 「Func&lt;> と ref / out 小咄」
 
Unreal engine4を使ったVRコンテンツ製作で 120%役に立つtips集+GDC情報をご紹介
Unreal engine4を使ったVRコンテンツ製作で 120%役に立つtips集+GDC情報をご紹介Unreal engine4を使ったVRコンテンツ製作で 120%役に立つtips集+GDC情報をご紹介
Unreal engine4を使ったVRコンテンツ製作で 120%役に立つtips集+GDC情報をご紹介
 
Boost container feature
Boost container featureBoost container feature
Boost container feature
 
dwangocpp1-lt
dwangocpp1-ltdwangocpp1-lt
dwangocpp1-lt
 
かわいくなろうとしたら語彙力が下がった話
かわいくなろうとしたら語彙力が下がった話かわいくなろうとしたら語彙力が下がった話
かわいくなろうとしたら語彙力が下がった話
 
C#言語機能の作り方
C#言語機能の作り方C#言語機能の作り方
C#言語機能の作り方
 
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
 
gitを使って、レポジトリの一部抽出forkしてみました
gitを使って、レポジトリの一部抽出forkしてみましたgitを使って、レポジトリの一部抽出forkしてみました
gitを使って、レポジトリの一部抽出forkしてみました
 
ゆるかわPhp
ゆるかわPhpゆるかわPhp
ゆるかわPhp
 
Ingress on GKE/GCE
Ingress on GKE/GCEIngress on GKE/GCE
Ingress on GKE/GCE
 
競技プログラミングのためのC++入門
競技プログラミングのためのC++入門競技プログラミングのためのC++入門
競技プログラミングのためのC++入門
 
中3女子でもわかる constexpr
中3女子でもわかる constexpr中3女子でもわかる constexpr
中3女子でもわかる constexpr
 
C#や.NET Frameworkがやっていること
C#や.NET FrameworkがやっていることC#や.NET Frameworkがやっていること
C#や.NET Frameworkがやっていること
 
Kotlinでマッチョする話
Kotlinでマッチョする話Kotlinでマッチョする話
Kotlinでマッチョする話
 
js-ctypes - ネイティブコードを呼び出す新しいカタチ
js-ctypes - ネイティブコードを呼び出す新しいカタチjs-ctypes - ネイティブコードを呼び出す新しいカタチ
js-ctypes - ネイティブコードを呼び出す新しいカタチ
 
PHP7をDockerで動かしたという話
PHP7をDockerで動かしたという話PHP7をDockerで動かしたという話
PHP7をDockerで動かしたという話
 
中3女子が狂える本当に気持ちのいい constexpr
中3女子が狂える本当に気持ちのいい constexpr中3女子が狂える本当に気持ちのいい constexpr
中3女子が狂える本当に気持ちのいい constexpr
 
C++の復習
C++の復習C++の復習
C++の復習
 

Tickライブラリで遊ぼう(C++)