-
Notifications
You must be signed in to change notification settings - Fork 6
/
c-tricks-1-1-conditional-operator.html
1182 lines (1115 loc) · 55.3 KB
/
c-tricks-1-1-conditional-operator.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-Hant"
>
<head>
<title>C++ Tricks 1.1 條件運算符(?:) - Farseerfc的小窩</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#6b5594">
<meta name="msapplication-navbutton-color" content="#6b5594">
<meta name="apple-mobile-web-app-status-bar-style" content="#6b5594">
<link rel="manifest" href="/manifest.json">
<link rel="apple-touch-icon" sizes="180x180" href="//farseerfc.me/images/apple-touch-icon.png">
<link rel="icon" type="image/png" href="//farseerfc.me/images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="//farseerfc.me/images/favicon-16x16.png" sizes="16x16">
<link rel="mask-icon" href="//farseerfc.me/images/safari-pinned-tab.svg" color="#603cba">
<link rel="canonical" href="//farseerfc.me/c-tricks-1-1-conditional-operator.html">
<meta name="author" content="farseerfc" />
<meta name="keywords" content="C++" />
<meta name="description" content="從 farseerfc.wordpress.com 導入 1.1 條件運算符(?:) 條件運算符(?:)是C++中唯一的三目運算符(trinary operator),用於在表達式中作條件判斷,通常可以替換if語句,與Visual Basic中的iif函數、Excel中的if函數有同樣的作用。語法形式如下: condition ? true_value : false_value 其中condition *條件是任何可以轉換爲bool類型的表達式,包括但不僅限於**bool*、int、指針。與if和while的條件部分稍顯不同的是,這裏不能定義變量,否則會導致語法錯誤。 另外,條件語句會切實地控制執行流程,而不僅僅是控制返回值。也就是說,兩個返回值表達式中永遠只有一個會被求值,在表達式的執行順序很重要時,這點尤爲值得注意。比如: int *pi=getInt(); int i=pi …" />
<meta property="og:site_name" content="Farseerfc的小窩" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="C++ Tricks 1.1 條件運算符(?:)"/>
<meta property="og:url" content="//farseerfc.me/c-tricks-1-1-conditional-operator.html"/>
<meta property="og:description" content="從 farseerfc.wordpress.com 導入 1.1 條件運算符(?:) 條件運算符(?:)是C++中唯一的三目運算符(trinary operator),用於在表達式中作條件判斷,通常可以替換if語句,與Visual Basic中的iif函數、Excel中的if函數有同樣的作用。語法形式如下: condition ? true_value : false_value 其中condition *條件是任何可以轉換爲bool類型的表達式,包括但不僅限於**bool*、int、指針。與if和while的條件部分稍顯不同的是,這裏不能定義變量,否則會導致語法錯誤。 另外,條件語句會切實地控制執行流程,而不僅僅是控制返回值。也就是說,兩個返回值表達式中永遠只有一個會被求值,在表達式的執行順序很重要時,這點尤爲值得注意。比如: int *pi=getInt(); int i=pi …"/>
<meta property="article:published_time" content="2007-08-22" />
<meta property="article:section" content="import" />
<meta property="article:tag" content="C++" />
<meta property="article:author" content="farseerfc" />
<meta property="og:image"
content="//farseerfc.me/c-tricks-1-1-conditional-operator.png"/>
<!-- Bootstrap -->
<link href="//farseerfc.me/theme/css/bootstrap.min.css" rel="stylesheet">
<link href="//farseerfc.me/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="//farseerfc.me/theme/css/pygments/monokai.css" rel="stylesheet">
<link href="//farseerfc.me/theme/tipuesearch/tipuesearch.css" rel="stylesheet">
<link href="//farseerfc.me/theme/css/html4css1.css" rel="stylesheet">
<link rel="stylesheet" href="//farseerfc.me/theme/css/style.css" type="text/css"/>
<link href="//farseerfc.me/feeds/atom.xml" type="application/atom+xml" rel="alternate"
title="Farseerfc的小窩 ATOM Feed"/>
<link href="//farseerfc.me/theme/css/material.min.css" rel="stylesheet">
<link href="//farseerfc.me/theme/css/ripples.css" rel="stylesheet">
<link href="//farseerfc.me/theme/css/github-markdown.css" rel="stylesheet">
</head>
<body>
<div style="display:none" id="title">C++ Tricks 1.1 條件運算符(?:) - Farseerfc的小窩</div>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">切換導航</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="//farseerfc.me/" class="navbar-brand">
Farseerfc的小窩 </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li class="dropdown hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="mdi-action-translate"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="active"><a href="//farseerfc.me/">繁體</a></li>
<li><a href="//farseerfc.me/jp/">日本語</a></li>
<li><a href="//farseerfc.me/en/">English</a></li>
<li><a href="//farseerfc.me/zhs/">简体</a></li>
</ul>
</li>
<ul class="nav navbar-nav hidden-xs hidden-md hidden-sm">
<li class="active"><a href="//farseerfc.me/"><i class="mdi-action-translate"></i>繁體</a></li>
<li><a href="//farseerfc.me/jp/"><i class="mdi-action-translate"></i>日本語</a></li>
<li><a href="//farseerfc.me/en/"><i class="mdi-action-translate"></i>English</a></li>
<li><a href="//farseerfc.me/zhs/"><i class="mdi-action-translate"></i>简体</a></li>
</ul>
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-user"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class=""><a href="//farseerfc.me/pages/about.html"><i class="fa fa-user"></i>
關於
</a></li>
<li class=""><a href="//farseerfc.me/pages/links.html"><i class="fa fa-user"></i>
友情鏈接
</a></li>
</ul>
</li>
<ul class="nav navbar-nav hidden-xs hidden-sm">
<li class=""><a href="//farseerfc.me/pages/about.html"><i class="fa fa-user"></i>
關於
</a></li>
<li class=""><a href="//farseerfc.me/pages/links.html"><i class="fa fa-user"></i>
友情鏈接
</a></li>
</ul>
</ul>
<ul class="nav navbar-nav hidden-md hidden-lg hidden-xl">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-folder-o"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="active">
<a href="//farseerfc.me/category/import.html"><i class="fa fa-folder-o"></i> Import</a>
</li>
<li >
<a href="//farseerfc.me/category/life.html"><i class="fa fa-folder-o"></i> Life</a>
</li>
<li >
<a href="//farseerfc.me/category/tech.html"><i class="fa fa-folder-o"></i> Tech</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav hidden-xs hidden-sm">
<li class="active">
<a href="//farseerfc.me/category/import.html"><i class="fa fa-folder-o"></i> Import</a>
</li>
<li >
<a href="//farseerfc.me/category/life.html"><i class="fa fa-folder-o"></i> Life</a>
</li>
<li >
<a href="//farseerfc.me/category/tech.html"><i class="fa fa-folder-o"></i> Tech</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right hidden-sm hidden-md hidden-lg hidden-xl">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-search"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><span>
<form class="navbar-search" action="/search.html">
<input type="text" class="search-query form-control col-lg-16" placeholder="搜索" name="q" id="tipue_search_input" required>
</form></span>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-right navbar-form hidden-xs">
<li><span>
<form class="navbar-search" action="/search.html">
<input type="text" class="search-query form-control col-lg-16" placeholder="搜索" name="q" id="tipue_search_input" required>
</form></span>
</li>
</ul>
<ul class="nav navbar-nav navbar-right hidden-xs">
<li><a href="//farseerfc.me/archives.html"><i class="fa fa-th-list"></i><span class="icon-label">歸檔</span></a></li>
<li><a href="//farseerfc.me/feeds/atom.xml" title="Atom feeds for all articles"><i class="fa fa-rss"></i></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<div class="container" style="min-height: 100%;height: auto !important;height: 100%;">
<div class="row" style="padding-bottom:80px;padding-top:80px;">
<div class="col-xl-21 col-lg-20 col-md-18">
<div id="loading-block">
<ol class="breadcrumb">
<li><a href="//farseerfc.me/" title="Farseerfc的小窩"><i class="fa fa-home fa-lg"></i></a></li>
<li><a href="//farseerfc.me/category/import.html" title="import">import</a></li>
<li class="active">C++ Tricks 1.1 條件運算符(?:)</li>
</ol>
<section id="content" class="article-content">
<article>
<header class="page-header jumbotron jumbotron-primary panel-primary" id="article-header">
<div class="panel-heading">
<h1>
C++ Tricks 1.1 條件運算符(?:)
<a href="//farseerfc.me/c-tricks-1-1-conditional-operator.html"
rel="bookmark"
class="btn btn-primary btn-lg"
title="到 C++ Tricks 1.1 條件運算符(?:) 的永久鏈接">
<i class="mdi-action-launch"></i>
</a>
</h1>
</div>
<div class="panel-body">
<div class="post-info">
<span class="published">
<time datetime="2007-08-22T18:05:00+09:00"><i class="fa fa-calendar"></i> 2007年08月22日(週三)</time>
</span>
<div class="btn-group translations">
<a href="javascript:void(0)" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"><i class="mdi-action-translate"></i><span class="caret"></a>
<ul class="dropdown-menu">
<li class="active"><a href="//farseerfc.me/c-tricks-1-1-conditional-operator.html">繁體</a></li><li><a href="//farseerfc.me/zhs/c-tricks-1-1-conditional-operator.html" class="translate">简体</a></li></ul>
</div>
<a onclick="$.get('//farseerfc.me/c-tricks-1-1-conditional-operator.rst.html', function(data){$('#source-code').html(data)});$('#article-content').toggle();$('#source-content').toggle();" class="btn btn-primary" title="顯示這篇文章的源文件"><i class="fa fa-code"></i></a>
<a href="//farseerfc.me/c-tricks-1-1-conditional-operator.pdf" class="btn btn-primary" title="顯示這篇文章的印刷版PDF文件" target="_blank"><i class="fa fa-file-pdf-o"></i></a>
<a href="//farseerfc.me/c-tricks-1-1-conditional-operator.png" class="btn btn-primary" title="顯示這篇文章的截圖PNG文件" target="_blank""><i class="fa fa-file-photo-o"></i></a>
<small><span><a href="//farseerfc.me/c-tricks-1-1-conditional-operator.html#disqus_thread" class="btn btn-primary" data-disqus-identifier="c-tricks-1-1-conditional-operator" data-disqus-url="http://farseerfc.me/c-tricks-1-1-conditional-operator.html"><i class="fa fa-comments-o"></i></a></span></small><span class="btn-group">
<a href="//farseerfc.me/tag/c.html" class="btn btn-primary btn-xs"><i class="fa fa-tag"></i> C++</a>
</span>
</div><!-- /.post-info --> </div>
</header>
<div class="entry-content jumbotron" id="article-content">
<p>從 <a class="reference external" href="http://farseerfc.wordpress.com/">farseerfc.wordpress.com</a> 導入</p>
<div class="section" id="id1">
<h2>1.1 條件運算符(?:)</h2>
<p>條件運算符(?:)是C++中唯一的三目運算符(trinary
operator),用於在表達式中作條件判斷,通常可以替換if語句,與Visual
Basic中的iif函數、Excel中的if函數有同樣的作用。語法形式如下:</p>
<p><em>condition</em> ? <em>true_value</em> : <em>false_value</em></p>
<p>其中<em>condition
*條件是任何可以轉換爲bool類型的表達式,包括但不僅限於**bool*</em>、<strong>int</strong>、指針。與<strong>if</strong>和<strong>while</strong>的條件部分稍顯不同的是,這裏不能定義變量,否則會導致語法錯誤。</p>
<p>另外,條件語句會切實地控制執行流程,而不僅僅是控制返回值。也就是說,兩個返回值表達式中永遠只有一個會被求值,在表達式的執行順序很重要時,這點尤爲值得注意。比如:</p>
<p><strong>int</strong> *pi=getInt();</p>
<p><strong>int</strong> i=pi<strong>?</strong>*pi<strong>:</strong>0;</p>
<p>這裏,只有當pi的值不爲0時,它纔會被提領(dereference)。這種語義保證了程序的正確性,因爲提領一個空指針將導致致命的運行期錯誤(通常是非法操作的警告)。同時,正因爲條件運算符控制運算流程的特點,使得它不能用類似iif的普通函數來模擬:</p>
<p><strong>int</strong> iif(<strong>int</strong> con,<strong>int</strong> t,<strong>int</strong>f){<strong>if</strong>(c)<strong>return</strong> t;<strong>return</strong> f;}//試圖模擬?:</p>
<p>…//in some function</p>
<p><strong>int</strong> *pi=getInt();</p>
<p><strong>int</strong> i=iif(pi,*pi,0);//Error!</p>
<p>這段代碼會導致上文提到的致命運行期錯誤。C/C++標準規定,參數在被傳遞給函數之前求值,因此無論pi爲何值,都會被提領。又因爲函數傳回一個空指針的情況比較少見,所以這樣的錯誤在調試時很難被發現,一旦發生又勢必造成重大災難。這樣的代碼在實踐中應儘量避免。</p>
<p>有時,條件運算符控制流程的特點會不知不覺影響我們的代碼。在C時代,最大值MAX通常用宏實現:</p>
<p><strong>#define</strong>MAX(a,b) ((a)>(b)<strong>?</strong>(a)<strong>:</strong>(b))</p>
<p>需要用額外的括號將宏參數和宏本體保護起來,以免運算符優先級擾亂邏輯,這是宏醜陋的特點之一,這裏暫且不提。矛盾在於,用具有副作用的表達式調用宏時,會出現問題:</p>
<p><strong>int</strong> i=5,j=6;//…</p>
<p><strong>int</strong> a=MAX(++i,++j);</p>
<p>代碼的作者原意顯然是想先將i,j分別遞增,再將其中較大的一個賦給a。執行這段代碼,當i=5,j=6時,a=8,知道爲什麼嗎?通過宏展開,賦值語句成這樣:</p>
<p><strong>int</strong> a=(++i)>(++j)<strong>?</strong>(++i)<strong>:</strong>(++j);//刪除了多餘括號</p>
<p>在判斷之前,i、j被分別自增一次,然後捨棄:之前的部分,j又被自增一次。執行之後,i=6,j=8。</p>
<p>MAX的更正確更安全的實現,是利用模板將類型參數化。STL標準算法中就有一個這樣的工具級模版函數std::max。</p>
<p>條件運算符是表達式而不是語句,這使得它可以出現在任何需要表達式的地方,這擴大了它的適用範圍。在那些語法上只能出現表達式而不能出現語句的地方(比如變量初始化),條件運算符有着不可替代的作用。</p>
<p>條件運算符優於<strong>if</strong>語句的另一個場合是“模板元編程”(TMP, Template
MetaProgramming)。在TMP這個古怪奇異的編譯期運算編程技術中,一切舊有的技術和法則被全線擊破,我們所能仰仗的工具,只有模板特化(Specialization)、<strong>typedef</strong>s、函數聲明(無法調用它們)、以及編譯期常量運算。已經有人很深入地論證過,僅有以上這些,就已經形成了一個“圖靈完善”的計算機語言。我們可以用模板特化技術,來模擬條件分支,循環迭代等一系列複雜的語言結構。由於可以參與編譯期常量運算,條件運算符在TMP世界中很自然地扮演起重要角色。</p>
<p>比如,給與類型T的一個變量t,我們想聲明一個緩衝區存放t和一個int,緩衝區的大小不小於sizeof(T)也不小於sizeif(int),我們可以這樣寫:</p>
<p>char buffer[sizeof(T)>sizeof(int)? sizeof(T): sizeof(int)];</p>
<p>我們不能用一個if語句替換這個運算:</p>
<p>int i;</p>
<p>if(sizeof(T)>sizeof(int))i=sizeof(T);</p>
<p>else i=sizeof(int);</p>
<p>char buffer[i];//語法錯誤!</p>
<p>原因在於數組聲明中的下標必須是一個編譯期常量,而不是一個運行期的值,條件表達式的運算可以在編譯期進行,if語句就只能在執行期執行。</p>
</div>
</div>
<div class="entry-content jumbotron" id="source-content" style="display: none">
<!-- <pre><code id="source-code">
</code></pre> -->
<div id="source-code"></div>
</div>
<!-- /.entry-content -->
<div class="row" id="prevnext">
<div class="col-xs-12">
<a href="//farseerfc.me/filling-believings-calling-conscience.html" class="btn btn-default btn-lg" style="float:left;clear:both;background-color:#fff;">
<h4><i class="fa fa-arrow-left"></i>
填補信仰、喚醒良知
</h4>
</a>
</div>
<div class="col-xs-12">
<a href="//farseerfc.me/c-tricks-1-2-trap-in-comma-logical-operator.html" class="btn btn-default btn-lg" style="float:right;clear:both;background-color:#fff;">
<h4>
C++ Tricks 1.2 逗號運算符(,)、邏輯運算符(&&,||)與運算符重載的陷阱<i class="fa fa-arrow-right"></i>
</h4>
</a>
</div>
</div>
<div class="panel panel-default" id="series">
<div class="panel-heading">
<h4>
這篇文章是 "CPP_Tricks" 系列文章的第 2 篇: </h4>
</div>
<ul class="list-group">
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks.html' style="text-align:left">C++ Tricks</a></li>
<li class="list-group-item"><a class="btn btn-primary" href='//farseerfc.me/c-tricks-1-1-conditional-operator.html' style="text-align:left">C++ Tricks 1.1 條件運算符(?:)</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-1-2-trap-in-comma-logical-operator.html' style="text-align:left">C++ Tricks 1.2 逗號運算符(,)、邏輯運算符(&&,||)與運算符重載的陷阱</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-2-1-x86-architecture.html' style="text-align:left">C++ Tricks 2.1 X86概述</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-2-2-i386-memory-layout.html' style="text-align:left">C++ Tricks 2.2 I386平臺的內存佈局</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-2-3-i386-stack-allocation-in-c-functions.html' style="text-align:left">C++ Tricks 2.3 I386平臺C函數內部的棧分配</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-2-4-i386-stack-allocation-accross-function-invocation.html' style="text-align:left">C++ Tricks 2.4 I386平臺C函數調用邊界的棧分配</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-2-5-address-alignment.html' style="text-align:left">C++ Tricks 2.5 I386平臺的邊界對齊(Align)</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-2-6-i386-variable-arguments.html' style="text-align:left">C++ Tricks 2.6 I386平臺C函數的可變參數表(Variable Arguments)</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-2-7-i386-calling-conventions.html' style="text-align:left">C++ Tricks 2.7 I386平臺的其它函數調用模型</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-3-1-lvalue-rvalue-constant.html' style="text-align:left">C++ Tricks 3.1 左值右值與常量性(lvalue,rvalue & constant)</a></li>
<li class="list-group-item"><a class="btn btn-default" href='//farseerfc.me/c-tricks-3-2-label-goto-and-implementation-of-switch.html' style="text-align:left">C++ Tricks 3.2 標號、goto,以及switch的實現</a></li>
</ul>
</div>
<section class="comments" id="comments">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-primary">
<div class="panel-heading" role="tab" id="githubissue-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#githubissue-comments" aria-expanded="true" aria-controls="githubissue-comments">
<i class="fa fa-comments-o"></i> Github Issue 留言 </a>
</h4>
</div>
<div id="githubissue-comments" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="githubissue-heading">
<div class="panel-body">
<div class="tab-pane fade active in" id="githubissue-comments">
請用你的 GitHub 賬戶登錄並在<a class="btn btn-primary withripple" href="https://github.com/farseerfc/farseerfc.github.io/issues/15#new_comment_field" target="_blank">這篇文章的 Issue 頁下</a>留言 <div id="githubissue_comments"></div>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var reaction_fa = {
"+1": "thumbs-o-up",
"-1": "thumbs-o-down",
"laugh": "smile-o",
"hooray": "gift",
"confused": "frown-o",
"heart": "heart-o"
};
function loadComments(data) {
for (var i=0; i<data.length; i++) {
var cuser = data[i].user.login;
var cuserlink = "https://github.com/" + data[i].user.login;
var clink = "https://github.com/farseerfc/farseerfc.github.io/issues/15#issuecomment-" + data[i].url.substring(data[i].url.lastIndexOf("/")+1);
var cbody = data[i].body_html || data[i].body;
var cavatarlink = data[i].user.avatar_url;
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour:'2-digit', minute:'2-digit', second:'2-digit' };
var cdate = new Date(data[i].created_at).toLocaleString("zh-Hans", options);
var reactions = data[i].reactions;
var creactions = "";
if(reactions){
for(reactionkey in reaction_fa){
if(reactions[reactionkey] > 0) {
creactions += '<i class="fa fa-'+ reaction_fa[reactionkey]+'"></i>' + reactions[reactionkey] + " ";
}
}
}
$("#githubissue_comments").append("<div class='media'><div class='media-left'><a href=\""+ cuserlink +'\"><img src="' + cavatarlink + '" width="48" class="media-object">' + "</a></div><div class='media-body'><h4 class='media-heading'><a class='commentuser' href=\""+ cuserlink + "\">" + cuser + "</a><small> 留言於 <a class='commentdate' href=\"" + clink + "\">" + cdate + "</a></small> "+ creactions + "</h4><div class='commentbody markdown-body'>" + cbody + "</div></div>");
$(".email-hidden-toggle > a").on("click", function (e){
e.preventDefault();
$(".email-hidden-reply", this.parent).toggle();
});
}
}
$.ajax("https://api.github.com/repos/farseerfc/farseerfc.github.io/issues/15/comments", {
headers: {Accept: "application/vnd.github.squirrel-girl-preview.full+json"},
dataType: "json",
success: function(msg){
loadComments(msg);
}
});
});
</script>
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading" role="tab" id="disqus-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#disqus-comments" aria-expanded="false" aria-controls="disqus-comments">
<i class="fa fa-comments-o"></i> Disqus 留言 </a>
</h4>
</div>
<div id="disqus-comments" class="panel-collapse collapse" role="tabpanel" aria-labelledby="disqus-heading">
<div class="panel-body">
<div class="tab-pane fade active in" id="disqus-comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'farseerfcgithub'; // required: replace example with your forum shortname
var disqus_identifier = 'c-tricks-1-1-conditional-operator';
var disqus_url = 'http://farseerfc.me/c-tricks-1-1-conditional-operator.html';
var disqus_config = function () {
this.language = "zh";
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
</div>
</div>
</div>
</div>
</section> </article>
</section>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<a data-dismiss="modal" href="javascript:void(0);">
<img id="mimg" src="" style="width:100%;height:auto">
</a>
</div>
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
<div class="col-xl-3 col-lg-4 col-md-6" id="sidebar">
<aside>
<section>
<div class="sidebar-container">
<div class="sidebar-item ">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="fa fa-user fa-lg"></i>
<a href="//farseerfc.me/pages/about.html">
關於 farseerfc</a>
</h4>
</div>
<div class="panel-body" id="aboutme">
<a href="//farseerfc.me/pages/about.html"><img width="100%" src="//farseerfc.me/../images/avatar.jpg"/></a>
<h3 style="text-align:center">
<a href="https://sak.uy/" target="_blank">
<i class="fa fa-music" style="text-align:center"></i></a>
<a href="https://twitter.com/farseerfc" target="_blank">
<i class="fa fa-twitter" style="text-align:center"></i></a>
<a href="https://github.com/farseerfc" target="_blank">
<i class="fa fa-github" style="text-align:center"></i></a>
<a href="http://www.facebook.com/farseerfc" target="_blank">
<i class="fa fa-facebook" style="text-align:center"></i></a>
<a href="https://plus.google.com/u/0/+JiachenYang/posts" target="_blank">
<i class="fa fa-google-plus" style="text-align:center"></i></a>
<a href="mailto:farseerfc@gmail.com" target="_blank">
<i class="mdi-communication-email" style="text-align:center"></i></a>
</h3>
</div>
</div>
</div>
<div class="sidebar-item hidden-xs">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<a href="//farseerfc.me/tags.html"><i class="fa fa-tags fa-lg"></i><span class="icon-label">標籤雲</span></a>
</h4>
</div>
<div class="panel-body">
<ul class="list-group list-inline tagcloud" id="tags">
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/swap.html">
swap <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/mm.html">
mm <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/memory-management.html">
memory management <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/swappiness.html">
swappiness <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/kswapd.html">
kswapd <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/watermark.html">
watermark <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/watermark_scale_factor.html">
watermark_scale_factor <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/cgroup-v2.html">
cgroup v2 <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/translate.html">
translate <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="//farseerfc.me/tag/fsbi-ji.html">
FS筆記 <sup> 5</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="//farseerfc.me/tag/fs-notes.html">
FS notes <sup> 5</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/file.html">
file <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/size.html">
size <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/histogram.html">
histogram <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/ftl.html">
FTL <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/ssd.html">
SSD <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/usb-stick.html">
USB stick <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/emmc.html">
eMMC <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/mmc.html">
MMC <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/sd-card.html">
SD Card <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/chs.html">
CHS <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/cylinder.html">
cylinder <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/head.html">
head <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/sector.html">
sector <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/lba.html">
LBA <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/smr.html">
SMR <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/4kn.html">
4Kn <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/btrfs.html">
btrfs <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/zfs.html">
zfs <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/cow.html">
cow <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/snapshot.html">
snapshot <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/clone.html">
clone <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/subvolume.html">
subvolume <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/dedup.html">
dedup <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/reflink.html">
reflink <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/spa.html">
SPA <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/dmu.html">
DMU <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/dsl.html">
DSL <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/zpl.html">
ZPL <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/layered.html">
layered <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/architecture.html">
architecture <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/zio.html">
ZIO <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/vdev.html">
VDEV <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/arc.html">
ARC <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/zap.html">
ZAP <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/zil.html">
ZIL <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/zvol.html">
ZVOL <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/friend.html">
friend <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/horo.html">
horo <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/life.html">
life <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="//farseerfc.me/tag/linux.html">
linux <sup> 9</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/archlinux.html">
archlinux <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/raspberrypi.html">
raspberrypi <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/usbip.html">
usbip <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/usb.html">
usb <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/forward.html">
forward <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/chouyaku.html">
chouyaku <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/kimi.html">
kimi <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/sae.html">
sae <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/inakerya.html">
inakerya <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/ting-yi.html">
聽譯 <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/dotfiles.html">
dotfiles <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/stow.html">
stow <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/zhihu.html">
zhihu <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/gplv3.html">
GPLv3 <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/licenses.html">
licenses <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/linus-torvalds.html">
Linus Torvalds <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/gpl.html">
GPL <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/fsf.html">
FSF <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/eff.html">
EFF <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="//farseerfc.me/tag/c.html">
C <sup> 15</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="//farseerfc.me/tag/pelican.html">
pelican <sup> 5</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="//farseerfc.me/tag/github.html">
github <sup> 3</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/pages.html">
pages <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/issues.html">
issues <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="//farseerfc.me/tag/python.html">
python <sup> 5</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/pacvis.html">
pacvis <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/pacman.html">
pacman <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/arch.html">
arch <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/pacgraph.html">
pacgraph <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/wayland.html">
wayland <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/xorg.html">
xorg <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/compositor.html">
compositor <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/desktop.html">
desktop <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/macosx.html">
macosx <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/windows.html">
windows <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/window.html">
window <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/manager.html">
manager <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/blog.html">
blog <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/writing.html">
writing <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/travis.html">
travis <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/travis-ci.html">
travis-ci <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/ubuntu.html">
ubuntu <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/japan.html">
japan <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="//farseerfc.me/tag/academic.html">
academic <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/material.html">
material <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/icse.html">
icse <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/software.html">
software <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/microsoft.html">
microsoft <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="//farseerfc.me/tag/java.html">
java <sup> 2</sup>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="sidebar-item hidden-xs">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="fa fa-github fa-lg"></i><span class="icon-label">GitHub倉庫</span>
</h4>