Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
C++ FAQ++ #TechLunch
Search
Livesense Inc.
PRO
April 23, 2014
Technology
0
30
C++ FAQ++ #TechLunch
2012/02/22(水) @ Livesense TechLunch
発表者:桂 大介
Livesense Inc.
PRO
April 23, 2014
Tweet
Share
More Decks by Livesense Inc.
See All by Livesense Inc.
27新卒_総合職採用_会社説明資料
livesense
PRO
0
1.6k
27新卒_Webエンジニア職採用_会社説明資料
livesense
PRO
0
5.4k
株式会社リブセンス・転職会議 採用候補者様向け資料
livesense
PRO
0
140
株式会社リブセンス 会社説明資料(報道関係者様向け)
livesense
PRO
0
1.6k
データ基盤の負債解消のためのリプレイス
livesense
PRO
0
520
26新卒_総合職採用_会社説明資料
livesense
PRO
0
12k
株式会社リブセンス会社紹介資料 / Invent the next common.
livesense
PRO
2
48k
26新卒_Webエンジニア職採用_会社説明資料
livesense
PRO
1
13k
中途セールス職_会社説明資料
livesense
PRO
0
280
Other Decks in Technology
See All in Technology
AgentCore BrowserとClaude Codeスキルを活用した 『初手AI』を実現する業務自動化AIエージェント基盤
ruzia
7
2k
Connection-based OAuthから学ぶOAuth for AI Agents
flatt_security
0
460
Snowflake導入から1年、LayerXのデータ活用の現在 / One Year into Snowflake: How LayerX Uses Data Today
civitaspo
0
2.6k
Claude Skillsの テスト業務での活用事例
moritamasami
1
120
Everything As Code
yosuke_ai
0
120
小さく、早く、可能性を多産する。生成AIプロジェクト / prAIrie-dog
visional_engineering_and_design
0
150
2025年の医用画像AI/AI×medical_imaging_in_2025_generated_by_AI
tdys13
0
190
Bedrock AgentCore Memoryの新機能 (Episode) を試してみた / try Bedrock AgentCore Memory Episodic functionarity
hoshi7_n
2
2.1k
[Data & AI Summit '25 Fall] AIでデータ活用を進化させる!Google Cloudで作るデータ活用の未来
kirimaru
0
4.1k
ESXi のAIOps だ!2025冬
unnowataru
0
440
re:Invent2025 セッションレポ ~Spec-driven development with Kiro~
nrinetcom
PRO
1
120
普段使ってるClaude Skillsの紹介(by Notebooklm)
zerebom
8
2.5k
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
We Have a Design System, Now What?
morganepeng
54
7.9k
A Soul's Torment
seathinner
1
2k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
280
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
51
Agile that works and the tools we love
rasmusluckow
331
21k
Un-Boring Meetings
codingconduct
0
170
The SEO Collaboration Effect
kristinabergwall1
0
310
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
Transcript
C++ FAQ++
[email protected]
reinterpret_castはどこで使うのか • 物理リソース(外部デバイスの制御レジスタなど) とそれらの最も基本的なソフトウェア制御 • これらは特定のアドレスに依存する Device_driver* p = reinterpret_case<Device_driver*>(0xffb8);
#include <iostream> class Foo { public: Foo(int n): _n(n) {}
int get() { return _n; } private: int _n; }; int get_foo_address() { int foo_address = reinterpret_cast<int>(new Foo(11211)); return foo_address + 3306; } int main() { int foo_address = get_foo_address() - 3306; Foo* foo = reinterpret_cast<Foo*>(foo_address); std::cout << foo->get() << std::endl; // 11211 return 0; }
const_castはどこで使うのか • 値を変更しないのにconst指定されてない関数 にconstを渡すとき • constメンバ関数と非constメンバ関数でコードを 共通化するとき
void cout_n(int n) { cout << n << endl; }
void cout_const_pn(const int* n) { cout << *n << endl; } void cout_mutable_pn(int* n) { cout << *n << endl; } int main() { const int* n = new int(123); cout_n(*n); cout_const_pn(n); cout_mutable_pn(const_cast<int*>(n)); return 0; }
const Variable & Function variable • const int n =
1; pointer • const int* n = new int(1); • int* const n = new int(1); • const int* const n = new int(1); function • const int* const plus3(const int n)
const Member function • constなオブジェクトに対して使われる • 不変性を約束するメンバ関数
class TextBlock { public: TextBlock(std::string t): text(t) {} const char&
operator[] (std::size_t position) const { return text[position]; } char& operator[] (std::size_t position) { return text[position]; } private: std::string text; }; int main() { TextBlock tb("Hello"); const TextBlock ctb("Hello"); tb[1] = 'a'; ctb[1] = 'a'; // Error! std::cout << tb[1] << std::endl; std::cout << ctb[1] << std::endl; return 0; }
class TextBlock { public: TextBlock(std::string t): text(t) {} const char&
operator[] (std::size_t position) const { return text[position]; } char& operator[] (std::size_t position) { return const_cast<char&>( static_cast<const TextBlock&>(*this)[position] ); } private: std::string text; }; int main() { TextBlock tb("Hello"); const TextBlock ctb("Hello"); tb[1] = 'a'; ctb[1] = 'a'; // Error! std::cout << tb[1] << std::endl; std::cout << ctb[1] << std::endl; return 0; }
mutableとは何か • constがついたオブジェクトでもmutableがついて いるメンバ変数は変更可能 • 「論理的不変性」を保持するために使用 private: mutable int n;
RTTI(実行時型情報) • Run-Time Type Identification • typeid(*p).name(); でクラス名が取れる • get_classとかinstanceof的なもの
• 多重継承や例外と同様多くの議論を招いた 反対派 • 要らない。C++の精神に反している。複雑で難し い。 賛成派 • 一部の人にとっては重要。使わない人には無 害。実装しないと勝手実装される。
RTTI(Googleコーディング規約) • ユニットテストならOK • それ以外では仮想関数・ダブルディスパッチを 使うこと • If you think
you truly cannot use those ideas, you may use RTTI. But think twice about it. :- ) Then think twice again.
スマートポインタ(おさらい) • deleteを呼ばなくてもスコープから外れたら削除 してくれるようにするオブジェクトポインタのラッ パー • 標準のstd::auto_ptr • boostのboost::shared_ptr (参照カウント)
• C++11からはshared_ptrがstdに
template<class T> class smart_ptr { public: smart_ptr(): ptr(0) {} smart_ptr(T*
ptr): ptr(ptr) {} ~smart_ptr() { delete ptr; } T& operator*() const { return *ptr; } T* operator->() const { return ptr; } private: T* ptr; }; template<class T> smart_ptr<T> smartptr(T *p) { smart_ptr<T> ptr = smart_ptr<T>(p); return ptr; }
スマートポインタ(参照カウント)の作り方 • の前に...
参照カウント(Reference Counted)とは $a = "foo"; // refcount=1 • $b =
$a; // refcount=2 • $c = $a; // refcount=3 • unset($c); // refcount=2 • unset($b); // refcount=1 • unset($a); // refcount=0 -> 値破棄
参照カウントとは - その2 • シンプルかつ高速なGC機構 • すぐ破棄される • 様々な言語で使われている •
Perl, PHP, Pythonなど(RubyはM&S) • 循環に弱い ◦ $a->b = $a; $b->a = $a;
template <class T> class Ptr { T *p; int *cnt;
public: Ptr(T *p) : p(p) { cnt = new int(1); cout << "count=" << *cnt << endl; } Ptr(const Ptr<T> &self) { p = self.p; cnt = self.cnt; (*cnt)++; cout << "count=" << *cnt << endl; } ~Ptr() { (*cnt)--; cout << "count=" << *cnt << endl; if (*cnt < 1) { delete p; delete cnt; cout << "delete!" << endl; } } T *ptr() { return p; } };
template <class T> Ptr<T> foo(T *p) { cout << "in
foo()" << endl; Ptr<T> ptr = Ptr<T>(p); cout << "out foo()" << endl; return ptr; } int main() { { cout << "in loop1" << endl; Ptr<int> p1 = foo<int>(new int(100)); cout << *p1.ptr() << endl; { cout << "in loop2" << endl; Ptr<int> p2 = p1; cout << *p2.ptr() << endl; cout << "out loop2" << endl; } cout << "out loop1" << endl; } return 0; }
次回予告 • Database in depth