Your SlideShare is downloading. ×
0
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Episteme variadic template
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Episteme variadic template

292

Published on

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

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

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
292
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. わんくま同盟茶藝部顧問 episthmh episteme@cppll.jp Microsoft MVP for Visual C++ (2004~)
  • 2.  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
  • 3.  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は型ではない
  • 4.  Function parameter pack  0個以上の値の並び template<class ...Ts> void f(Ts ...vs); f(); f(1); f(2, 1.0); // vs は空 // vs はintひとつの並び // vs はintとdoubleの並び
  • 5. typedef Ts MyList; // error! Ts var; // error! auto copy = vs; // error!
  • 6.  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)...); }
  • 7. 書式 展開 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)
  • 8. template<typename ...Ts> void f(Ts ...vs) { g(A<Ts...>::h(vs)...); g(A<Ts...>::h(vs...)); g(A<Ts>::h(vs)...); }
  • 9. 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)); }
  • 10.  キモは「パターン・マッチング」 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) );
  • 11.  キモはやっぱり「パターン・マッチング」 // 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...> {};
  • 12.  キモは「パターン・マッチング」 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"; }

×