• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Episteme variadic template
 

Episteme variadic template

on

  • 329 views

- 2013.11.02 C++例外安全Day@大阪

- 2013.11.02 C++例外安全Day@大阪
- 2013.11.09 わんくま同盟東京勉強会#85
- 2013.11.14 歌舞伎座.tech #2

Statistics

Views

Total Views
329
Views on SlideShare
327
Embed Views
2

Actions

Likes
1
Downloads
2
Comments
0

1 Embed 2

http://nekko1119.hatenablog.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Episteme variadic template Episteme variadic template Presentation Transcript

    • わんくま同盟茶藝部顧問 episthmh episteme@cppll.jp Microsoft MVP for Visual C++ (2004~)
    •  variadic function お馴染み printf(const char* format, ...) printf(“I‟m %s, %d years old.”, “Adam”, 20);  variadic macro #define FL_printf(format,...) printf(“%s(%d) “ format, __FILE__, __LINE__, __VA_ARGS__) FL_printf(“I‟m %s, %d years old.”, “Adam”, 20);  …そして variadic template
    •  Template parameter pack  0個以上の型の並び template<class ...Ts> struct Tuple {}; Tuple<> t0; Tuple<int> t1; Tuple<int,float> t2; Tuple<0> error; // Tsは 空 // intひとつの並び // intとfloatの並び // エラー:0は型ではない
    •  Function parameter pack  0個以上の値の並び template<class ...Ts> void f(Ts ...vs); f(); f(1); f(2, 1.0); // vs は空 // vs はintひとつの並び // vs はintとdoubleの並び
    • typedef Ts MyList; // error! Ts var; // error! auto copy = vs; // error!
    •  sizeof... size_t items = sizeof...(Ts); size_t items = sizeof...(vs);  packの展開 template<typename ...Ts> void f(Ts&& ...vs) { g(3.14, std::forward<Ts>(vs)...); }
    • 書式 展開 Ts... Ts&&... X<Ts,Y>::z... X<Ts,Us>... F(5, vs)... T1, … , Tn T1&&, … , Tn&& X<T1,Y>::z, … , X<Tn,Y>::z X<T1,U1>, … , X<Tn,Un> F(5,v1), … , f(5,vn)
    • template<typename ...Ts> void f(Ts ...vs) { g(A<Ts...>::h(vs)...); g(A<Ts...>::h(vs...)); g(A<Ts>::h(vs)...); }
    • f(12, „3‟); すると… void f(int v1, char v2) { // g(A<Ts...>::h(vs)...); g(A<int,char>::h(v1), A<int,char>::h(v2)); // g(A<Ts...>::h(vs...)); g(A<int,char>::h(v1,v2)); // g(A<Ts>::h(vs)...); g(A<int>::h(v1), A<char>::h(v2)); }
    •  キモは「パターン・マッチング」 template<class T1, class T2> bool isOneOf(T1&& a, T2&& b) { return a == b; } template<class T1, class T2, class ...Ts> bool isOneOf(T1&& a, T2&& b, Ts&& ...vs) { return a == b || isOneOf(a, vs...); } assert( isOneOf(1, 2, 3.5, 4, 1, 2) );
    •  キモはやっぱり「パターン・マッチング」 // tuple<T0,T1,T2...> : // tuple<char,int,const char*> t; get<0>(t) = „A‟; get<1>(t) = 12; … template<int N, typename ...Ts> struct tuple_data; template<int N> struct tuple_data<N> {}; template<int N, typename Thead, typename ...Tbody> struct tuple_data<N, Thead, Tbody...> : public tuple_data<N + 1, Tbody...> { Thead value; }; template<typename ...Ts> struct tuple : public tuple_data<0, Ts...> {};
    •  キモは「パターン・マッチング」 template<int N, typename Thead, typename ...Tbody> Thead& get_tuple_data(tuple_data<N, Thead, Tbody...>& t) { return t.value; } template<int N, typename ...Ts> auto get(tuple<Ts...>& t) -> decltype(get_tuple_data<N>(t)) { return get_tuple_data<N>(t); } /* おためし */ int main() { tuple<char,int,const char*> t; get<0>(t) = 'T'; get<1>(t) = 4; get<2>(t) = "two"; }