SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
CAUTION
この発表は黒魔術要素が多く含まれています。
内容はあまり真に受けず、
プロダクトコードには用法・用量をよく守って
お使いください。
Intoroduce to CPP
~黒魔術へのいざない~
2018/06/19 Mixi Jikai Lightning Talks #1
@reanisz__
C++3つの世界
テンプレート
世界
プリプロセス
世界
コンパイルされたものを実行したときの世界。
チューリング完全。
コンパイル時にテンプレートを解決する世界。
チューリング完全。
コンパイル前にソースファイルを処理する世界。
(※条件付きで)チューリング完全。
実行時
世界
(人間界)
※プリプロセッサ自体をループ内で実行したとき
もっと
プリプロセス世界も
使いこなすべき!
※個人の感想であり、効果・効能を示すものではありません。
マクロおさらい
#define HOGE AAAAAA
HOGE // AAAAAA に置換される
#define FUNC(a, b) a b a
FUNC(X, Y) // X Y X に置換される
#define CAT(a, b) a ## b
CAT(X, Y) // トークン連結して XY に置換される
#define STRINGIZE(a) #a
STRINGIZE(HOGE) // 文字列化して"Hoge"に置換される
#define FUNC2(a, ...) a __VA_ARGS__ a
FUNC2(A, X, Y, Z) // A X, Y, Z A に置換される (可変長引数)
マクロは再帰できない
#define Func(x) x(y)
Func(Func)
// => Func(y)
// ここまで展開されるが、そこで止まる (再帰できない) (重要)
// => y(y)
Enumの文字列化
こんなコードあるあるですよね
enum Type{
Dog,
Cat,
Human,
};
const char* getName(Type t){
switch(t){
case Dog:
return "Dog";
case Cat:
return "Cat";
case Human:
return "Human";
}
}
getNameを追加し忘れたら…
enum Type{
Dog,
Cat,
Human,
Dragon,
};
const char* getName(Type t){
switch(t){
case Dog:
return "Dog";
case Cat:
return "Cat";
case Human:
return "Human";
}
}
プリプロセスで
自動生成しましょう!
CodeGen 完成形イメージ
enum Type{
Dog,
Cat,
Human,
};
const char* getName(Type t){
switch(t){
case Dog:
return "Dog";
case Cat:
return "Cat";
case Human:
return "Human";
}
}
#define TYPES (Dog, Cat, Human)
#define GEN_ENUM(v) v,
enum Type{
FOR_EACH(GEN_ENUM, TYPES)
};
#define GEN_FUNC(v) ¥
case v : return STRINGIZE(v);
const char* getName(Type t){
switch(t){
FOR_EACH(GEN_FUNC, TYPES)
}
}
プリプロセスによるTuple
(a) // 1要素のtuple
(a, b) // 2要素のtuple
(a, b, c) // 3要素のtuple
Tupleの要素数を得るマクロ
#define TUPLE_SIZE_D(e0, e1, e2, e3, size, ...) size
#define TUPLE_SIZE_I(...) TUPLE_SIZE_D(__VA_ARGS__, 4, 3, 2, 1)
#define TUPLE_SIZE(tuple) TUPLE_SIZE_I tuple
TUPLE_SIZE( (a, b, c) )
// => TUPLE_SIZE_I (a, b, c)
// => TUPLE_SIZE_D (a, b, c, 4, 3, 2, 1)
// => 3
Tupleの特定の要素を得る
#define CAT(a, b) a ## b
#define TUPLE_GET_0(e0, ...) e0
#define TUPLE_GET_1(e0, e1, ...) e1
#define TUPLE_GET_2(e0, e1, e2, ...) e2
#define TUPLE_GET_3(e0, e1, e2, e3, ...) e3
#define TUPLE_GET_I(i, tuple) CAT(TUPLE_GET_, i) tuple
#define TUPLE_GET(i, tuple) TUPLE_GET_I(i, tuple)
TUPLE_GET(1, (a, b))
// => TUPLE_GET_I(1, (a, b))
// => CAT(TUPLE_GET_, 1) (a, b)
// => TUPLE_GET_1 (a, b)
// => b
繰り返す
#define REPEAT_I4(f, p, n) REPEAT_I3(f, p, n) f( p (3) n )
#define REPEAT_I3(f, p, n) REPEAT_I2(f, p, n) f( p (2) n )
#define REPEAT_I2(f, p, n) REPEAT_I1(f, p, n) f( p (1) n )
#define REPEAT_I1(f, p, n) f( p (0) n )
#define REPEAT_I(i, f, p, n) CAT(REPEAT_I, i)(f, p, n)
#define REPEAT(i, f, p, n) REPEAT_I(i, f, p, n)
REPEAT(3, F, P, N)
// => REPEAT_I(3, F, P, N)
// => CAT(REPEAT_I, 3)(F, P, N)
// => REPEAT_I3(F, P, N)
// => REPEAT_I2(F, P, N) F( P (2) N )
// => REPEAT_I1(F, P, N) F( P (1) N ) F( P (2) N )
// => F( P (0) N ) F ( P (1) N ) F ( P (2) N )
Foreachを書く
#define FOR_EACH_FUNC_I(i) CAT(TUPLE_GET_, i)
#define FOR_EACH(tuple, func) ¥
REPEAT(TUPLE_SIZE(tuple), func, FOR_EACH_FUNC_I, tuple)
FOR_EACH( (a, b, c), func )
// => REPEAT(TUPLE_SIZE((a, b, c)), func, FOR_EACH_FUNC_I, (a, b, c)
// => REPEAT(3, func, FOR_EACH_FUNC_I, (a, b, c)
// => func(FOR_EACH_FUNC_I (0) (a, b, c))
func(FOR_EACH_FUNC_I (1) (a, b, c))
func(FOR_EACH_FUNC_I (2) (a, b, c))
// => func(TUPLE_GET_0(a, b, c))
func(TUPLE_GET_1(a, b, c))
func(TUPLE_GET_2(a, b, c))
// => func(a) func(b) func(c)
完成!
#define TYPES (Dog, Cat, Human)
#define GEN_ENUM(v) v,
enum Type{
FOR_EACH(TYPES, GEN_ENUM)
};
#define GEN_FUNC(v) case v : return STRINGIZE(v);
const char* getName(Type t){
switch(t){
FOR_EACH(TYPES, GEN_FUNC)
}
}
完成!
#define TYPES (Dog, Cat, Human)
#define GEN_ENUM(v) v,
enum Type{
GEN_ENUM(Dog) GEN_ENUM(Cat) GEN_ENUM(Human)
};
#define GEN_FUNC(v) case v : return STRINGIZE(v);
const char* getName(Type t){
switch(t){
GEN_FUNC(Dog)
GEN_FUNC(Cat)
GEN_FUNC(Human)
}
}
FOR_EACH( (a, b, c), func )
// => func(a) func(b) func(c)
完成!
#define TYPES (Dog, Cat, Human)
#define GEN_ENUM(v) v,
enum Type{
Dog, Cat, Human,
};
#define GEN_FUNC(v) case v : return STRINGIZE(v);
const char* getName(Type t){
switch(t){
case Dog: return "Dog";
case Cat: return "Cat";
case Human: return "Human";
}
}
GEN_ENUM(v)
// => v,
GEN_FUNC(v)
// => case v: return “v”;
人間向け (Boost.PP)
#define TYPES (Dog)(Cat)(Human)
#define GEN_ENUM(v) v,
enum Type{
BOOST_PP_SEQ_FOR_EACH(GEN_ENUM, _, TYPES)
};
#define GEN_FUNC(v) case v : return STRINGIZE(v);
const char* getName(Type t){
switch(t){
BOOST_PP_SEQ_FOR_EACH(GEN_FUNC, _, TYPES)
}
}
結論
結論
Pythonでやれ
ご清聴
ありがとうございました

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Introduce to CPP ~黒魔術へのいざない~