-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPimp My Pimpl - Reloaded.html
1123 lines (984 loc) · 71 KB
/
Pimp My Pimpl - Reloaded.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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pimp My Pimpl — Reloaded | -Wmarc</title>
<link rel="pingback" href="https://marcmutz.wordpress.com/xmlrpc.php" />
<link rel="alternate" type="application/rss+xml" title="-Wmarc » Feed" href="https://marcmutz.wordpress.com/feed/" />
<link rel="alternate" type="application/rss+xml" title="-Wmarc » Comments Feed" href="https://marcmutz.wordpress.com/comments/feed/" />
<link rel="alternate" type="application/rss+xml" title="-Wmarc » Pimp My Pimpl — Reloaded Comments Feed" href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/feed/" />
<script type="text/javascript">
/* <![CDATA[ */
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function () {
oldonload();
func();
}
}
}
/* ]]> */
</script>
<link rel='stylesheet' id='all-css-0' href='https://s2.wp.com/_static/??-eJx9kd1qwzAMRl9onhgLKbsYexbHUV2ltiX8Q+jbV1nY1pLhG6FPnIMEglWM41QxVYjNSGieUoESKeBNMi/o6nN6daW8wP9aoCsWWLCKdVfznQ74FNj/Cew9ztyqOXMIvMJKs8fuDscZdR7F1o2IOJPFgFGxnhZl/LG29qIHHm974FdR2kyTZCzFaI3UoqkXXXT09jFIm0AjZslUEEq9BeztwMgL7XXHCBJXUrL8Nj3fI5vAzlbi9BTMOVjKPTXj9gdtPSj1EDfpK36+DcPH+H4aT8NyBxmtzhs=' type='text/css' media='all' />
<link rel='stylesheet' id='print-css-1' href='https://s1.wp.com/wp-content/mu-plugins/global-print/global-print.css?m=1444132114g' type='text/css' media='print' />
<link rel='stylesheet' id='all-css-2' href='https://s2.wp.com/_static/??-eJx9jEsOwjAMRC+EMZUilS4QZwmNcYzyU+yq12+RYMVnNU+aeYNrg7kWo2KYF2hpYSmKrarBPXnpqNF3KfzO46x6wO8WU6Eue/EDP1yLlEkxOuRUbz79O18lMJniPnkxcJcAvgRIovZ0r/kyODeNp3E4T48NEuFRhQ==' type='text/css' media='all' />
<script type='text/javascript'>
/* <![CDATA[ */
var LoggedOutFollow = {"invalid_email":"Your subscription did not succeed, please try again with a valid email address."};
/* ]]> */
</script>
<script type='text/javascript' src='https://s2.wp.com/_static/??-eJyFkOsKwjAMhV/IWDbnD3+Iz7JLVlJ7s2kt+vRWNwV1KAQSku8cDhHZA9lepwFZqFKnhOEyt7XilfgFgCEZ2ohrQ/YJ985GtPHOGteRRkiMoZVlV4xGt8B5x9Egc4EWru+RyJ4J819MYfRtf4SATNcv1047CV4nSZZFmSUOLkUYndYui0yDxPipMemlmIDHO6YRumQ8zMEOZl81zaauq121VTdrXIQQ'></script>
<link rel='stylesheet' id='all-css-0' href='https://s2.wp.com/wp-content/mu-plugins/highlander-comments/style.css?m=1377793621g' type='text/css' media='all' />
<!--[if lt IE 8]>
<link rel='stylesheet' id='highlander-comments-ie7-css' href='https://s2.wp.com/wp-content/mu-plugins/highlander-comments/style-ie7.css?m=1351637563g&ver=20110606' type='text/css' media='all' />
<![endif]-->
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://marcmutz.wordpress.com/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://s1.wp.com/wp-includes/wlwmanifest.xml" />
<meta name="generator" content="WordPress.com" />
<link rel="canonical" href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/" />
<link rel='shortlink' href='http://wp.me/P10053-4W' />
<link rel="alternate" type="application/json+oembed" href="https://public-api.wordpress.com/oembed/1.0/?format=json&url=https%3A%2F%2Fmarcmutz.wordpress.com%2Ftranslated-articles%2Fpimp-my-pimpl-%25e2%2580%2594-reloaded%2F&for=wpcom-auto-discovery" /><link rel="alternate" type="application/xml+oembed" href="https://public-api.wordpress.com/oembed/1.0/?format=xml&url=https%3A%2F%2Fmarcmutz.wordpress.com%2Ftranslated-articles%2Fpimp-my-pimpl-%25e2%2580%2594-reloaded%2F&for=wpcom-auto-discovery" />
<!-- Jetpack Open Graph Tags -->
<meta property="og:type" content="article" />
<meta property="og:title" content="Pimp My Pimpl — Reloaded" />
<meta property="og:url" content="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/" />
<meta property="og:description" content="This is a translation of a two-part article that originally appeared on Heise Developer. You can find the originals here: Part One: Part Two: You can find Part One here: Pimp My Pimpl — Reloaded Mu..." />
<meta property="article:published_time" content="2011-08-14T11:49:38+00:00" />
<meta property="article:modified_time" content="2011-08-26T15:58:19+00:00" />
<meta property="og:site_name" content="-Wmarc" />
<meta property="og:image" content="http://vg01.met.vgwort.de/na/58b4397d3b154577800ce7bd0c956208" />
<meta property="og:locale" content="en_US" />
<meta name="twitter:site" content="@wordpressdotcom" />
<meta name="twitter:image" content="http://vg01.met.vgwort.de/na/58b4397d3b154577800ce7bd0c956208?w=240" />
<meta name="twitter:card" content="summary" />
<meta property="fb:app_id" content="249643311490" />
<meta property="article:publisher" content="https://www.facebook.com/WordPresscom" />
<link rel="shortcut icon" type="image/x-icon" href="https://s2.wp.com/i/favicon.ico" sizes="16x16 24x24 32x32 48x48" />
<link rel="icon" type="image/x-icon" href="https://s2.wp.com/i/favicon.ico" sizes="16x16 24x24 32x32 48x48" />
<link rel="apple-touch-icon-precomposed" href="https://s0.wp.com/i/webclip.png" />
<link rel='openid.server' href='https://marcmutz.wordpress.com/?openidserver=1' />
<link rel='openid.delegate' href='https://marcmutz.wordpress.com/' />
<link rel="search" type="application/opensearchdescription+xml" href="https://marcmutz.wordpress.com/osd.xml" title="-Wmarc" />
<link rel="search" type="application/opensearchdescription+xml" href="https://wordpress.com/opensearch.xml" title="WordPress.com" />
<style>
@media screen and (min-width: 783px) {
#wpadminbar .quicklinks li#wp-admin-bar-my-account.with-avatar > a img {
margin-top: 5px;
}
}
</style>
<meta name="application-name" content="-Wmarc" /><meta name="msapplication-window" content="width=device-width;height=device-height" /><meta name="msapplication-tooltip" content="Marc Mutz on Qt, Boost, and C++" /><meta name="msapplication-task" content="name=Subscribe;action-uri=https://marcmutz.wordpress.com/feed/;icon-uri=https://s2.wp.com/i/favicon.ico" /><meta name="msapplication-task" content="name=Sign up for a free blog;action-uri=http://wordpress.com/signup/;icon-uri=https://s2.wp.com/i/favicon.ico" /><meta name="msapplication-task" content="name=WordPress.com Support;action-uri=http://support.wordpress.com/;icon-uri=https://s2.wp.com/i/favicon.ico" /><meta name="msapplication-task" content="name=WordPress.com Forums;action-uri=http://forums.wordpress.com/;icon-uri=https://s2.wp.com/i/favicon.ico" /><meta name="title" content="Pimp My Pimpl — Reloaded | -Wmarc on WordPress.com" />
<meta name="description" content="This is a translation of a two-part article that originally appeared on Heise Developer. You can find the originals here: Part One: http://www.heise.de/developer/artikel/C-Vor-und-Nachteile-des-d-Zeiger-Idioms-Teil-1-1097781.html Part Two: http://www.heise.de/developer/artikel/C-Vor-und-Nachteile-des-d-Zeiger-Idioms-Teil-2-1136104.html You can find Part One here: https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl/ Pimp My Pimpl — Reloaded Much has been written about this funnily-named idiom, alternatively known as d-pointer, compiler firewall, or Cheshire Cat.…" />
<style type="text/css">
#header {
background: transparent url(https://marcmutz.files.wordpress.com/2010/07/bird-feeding-gradient-3.jpg) no-repeat;
border: none;
}
</style>
<style type="text/css">
#header h1,
#header h1 a,
#header h1 a:visited,
#header h4,
#header h4 a,
#header h4 a:visited,
.header-left {
color: #000000;
}
</style>
<style type="text/css" id="syntaxhighlighteranchor"></style>
</head>
<body class="page page-id-306 page-child parent-pageid-227 page-template-default mp6 customizer-styles-applied highlander-enabled highlander-light">
<div id="header">
<div class="header-left">
<h4><a href="https://marcmutz.wordpress.com/">-Wmarc</a></h4>
<p id="description">Marc Mutz on Qt, Boost, and C++</p>
</div>
<div class="header-right">
<form method="get" id="searchform" action="https://marcmutz.wordpress.com/" >
<div><label class="hidden" for="s">Search:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Go" /></div>
</form>
</div>
</div>
<div id="access">
<div id="nav">
<div id="supernav" class="navleft nav">
<div class="menu"><ul><li ><a href="https://marcmutz.wordpress.com/">Home</a></li><li class="page_item page-item-2"><a href="https://marcmutz.wordpress.com/about/">About</a></li><li class="page_item page-item-23 page_item_has_children"><a href="https://marcmutz.wordpress.com/effective-qt/">Effective Qt</a><ul class='children'><li class="page_item page-item-26"><a href="https://marcmutz.wordpress.com/effective-qt/subclassing/">Don’t be sub-class when subclassing</a></li><li class="page_item page-item-543"><a href="https://marcmutz.wordpress.com/effective-qt/strings/">Four Strings to a Bow</a></li><li class="page_item page-item-256"><a href="https://marcmutz.wordpress.com/effective-qt/prefer-to-use-normalised-signalslot-signatures/">Prefer to use normalised signal/slot signatures</a></li><li class="page_item page-item-135"><a href="https://marcmutz.wordpress.com/effective-qt/containers/">Understand the Qt containers</a></li><li class="page_item page-item-29"><a href="https://marcmutz.wordpress.com/effective-qt/casting/">Coming Soon: Prefer qobject_cast() over isA()/inherits()</a></li><li class="page_item page-item-53"><a href="https://marcmutz.wordpress.com/effective-qt/properties/">Coming Soon: Property properly</a></li><li class="page_item page-item-54"><a href="https://marcmutz.wordpress.com/effective-qt/moc/">Coming Soon: Mocking moc</a></li></ul></li><li class="page_item page-item-471 page_item_has_children"><a href="https://marcmutz.wordpress.com/private-practice/">Private Practice</a><ul class='children'><li class="page_item page-item-474"><a href="https://marcmutz.wordpress.com/private-practice/private-practice-taming-templates/">Private Practice: Taming Templates</a></li><li class="page_item page-item-497"><a href="https://marcmutz.wordpress.com/private-practice/whats-in-a-proxy-style/">What’s in a Proxy-Style?</a></li></ul></li><li class="page_item page-item-227 page_item_has_children current_page_ancestor current_page_parent"><a href="https://marcmutz.wordpress.com/translated-articles/">Translated Articles</a><ul class='children'><li class="page_item page-item-229"><a href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl/">Pimp My Pimpl</a></li><li class="page_item page-item-306 current_page_item"><a href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/">Pimp My Pimpl — Reloaded</a></li></ul></li></ul></div>
</div>
<div class="navright">
<a class="rsslink" rel="nofollow" href="https://marcmutz.wordpress.com/feed/">Posts</a>
<a class="rsslink" rel="nofollow" href="https://marcmutz.wordpress.com/comments/feed/">Comments</a>
</div>
</div>
<div id="subnav" class="subnav nav">
<div class="menu">
<ul>
<li class="cat-item cat-item-3205"><a href="https://marcmutz.wordpress.com/category/english/" >English</a>
</li>
<li class="cat-item cat-item-2765"><a href="https://marcmutz.wordpress.com/category/german/" >German</a>
</li>
<li class="cat-item cat-item-2426"><a href="https://marcmutz.wordpress.com/category/programming-languages/c/" >C++</a>
</li>
<li class="cat-item cat-item-61037"><a href="https://marcmutz.wordpress.com/category/software-libraries/qt/" >Qt</a>
</li>
<li class="cat-item cat-item-214632"><a href="https://marcmutz.wordpress.com/category/software-libraries/boost/" >Boost</a>
</li>
<li class="cat-item cat-item-1068504"><a href="https://marcmutz.wordpress.com/category/programming-languages/c/c0x/" >C++0x</a>
</li>
<li class="cat-item cat-item-39389780"><a href="https://marcmutz.wordpress.com/category/columns/heise-developer/" >Heise Developer</a>
</li>
<li class="cat-item cat-item-39438656"><a href="https://marcmutz.wordpress.com/category/columns/effective-qt/" >Effective Qt</a>
</li>
<li class="cat-item cat-item-990667"><a href="https://marcmutz.wordpress.com/category/columns/private-practice/" >Private Practice</a>
</li>
<li class="cat-item cat-item-2227387"><a href="https://marcmutz.wordpress.com/category/projects/gpg4win/" >Gpg4win</a>
</li>
</ul>
</div>
</div>
</div>
<div id="wrap">
<div id="content">
<div id="content-left">
<div class="post-306 page type-page status-publish hentry">
<div class="entry">
<h1>Pimp My Pimpl — Reloaded</h1>
<p><img src="http://vg01.met.vgwort.de/na/58b4397d3b154577800ce7bd0c956208" width="1" height="1" alt=""><br />
This is a translation of a two-part article that originally appeared on <a href="http://www.heise.de/developer">Heise Developer</a>. You can find the originals here:</p>
<ul>
<li>Part One: <a href="http://www.heise.de/developer/artikel/C-Vor-und-Nachteile-des-d-Zeiger-Idioms-Teil-1-1097781.html">http://www.heise.de/developer/artikel/C-Vor-und-Nachteile-des-d-Zeiger-Idioms-Teil-1-1097781.html</a></li>
<li>Part Two: <a href="http://www.heise.de/developer/artikel/C-Vor-und-Nachteile-des-d-Zeiger-Idioms-Teil-2-1136104.html">http://www.heise.de/developer/artikel/C-Vor-und-Nachteile-des-d-Zeiger-Idioms-Teil-2-1136104.html</a></li>
</ul>
<p>You can find Part One here:</p>
<ul>
<li><a href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl/" title="Pimp My Pimpl">https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl/</a></li>
</ul>
<h1>Pimp My Pimpl — Reloaded</h1>
<p><strong>Much has been written about this funnily-named idiom, alternatively known as d-pointer, compiler firewall, or Cheshire Cat. After a first article on <i>Heise Developer</i> first presented the classic Pimpl Idiom and its benefits, this second part will focus on removing some of the drawbacks that inevitably occur when using Pimpls.</strong><span id="more-306"></span></p>
<h2>Part Two</h2>
<p><a></a></p>
<h3>The Shallow-<code>const</code> Problem</h3>
<p>A first gotcha, easily overlooked, has to do with C’s concept of shallow <code>const</code>. When using Pimpl, all methods access data fields of the class merely through <code>d</code>:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
SomeThing & Class::someThing() const {
return d->someThing;
}
</pre>
<p>Only a closer look reveals that the code evades a C++ security feature: Since the method is declared <code>const</code>, <code>this</code> inside <code>someThing()</code> is of type <code>const Class*</code>, hence <code>d</code> is of type <code>Class::Private * const</code>. That, however, does not suffice to prevent write access to data fields of <code>Class::Private</code>, because even though <code>d</code> is <code>const</code>, <code>*d</code> is not.</p>
<p>Remember: In C/C++, <code>const</code> is not deep, but shallow:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
const int * pci; // pointer to const int
int * const cpi; // const pointer to int
const int * const cpci; // const pointer to const int
*pci = 1; // error: *pci is const
*cpi = 1; // ok! *cpi isn't const
*cpci = 1; // error: *cpci is const
int i;
pci = &i; // ok
cpi = &i; // error: cpi is const
cpci = &i; // error: cpci is const
</pre>
<p>When using Pimpl, therefore, both <code>const</code> and non-<code>const</code> methods can write to the data field of the object. In the version without Pimpl, the compiler actively prevents that.</p>
<p>This hole in the type system will usually be undesirable and should be closed. This is possible using <code>deep_const_ptr</code> or a pair of <code>d_func()</code> methods. The former is a simple smart pointer class, which retrofits deep <code>const</code> for selected pointer variables. Its class definition, reduced to the essentials, might look as follows:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
template <typename T>
class deep_const_ptr {
T * p;
public:
explicit deep_const_ptr( T * t ) : p( t ) {}
const T & operator*() const { return *p; }
T & operator*() { return *p; }
const T * operator->() const { return p; }
T * operator->() { return p; }
};
</pre>
<p>By using the trick to overload <code>const</code> and non-<code>const</code> versions of <code>operator*()</code> and <code>operator-></code>, the constness of <code>d</code> is forwarded to <code>*d</code>. Simply replacing <code>Private * d;</code> with <code>deep_const_ptr<Private> d</code> closes the hole in an effective manner. But there’s no need for a smart pointer here: The <code>const</code>/non-<code>const</code> overloading trick also works with methods on <code>Class</code> directly:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
class Class {
// ...
private:
const Private * d_func() const { return _d; }
Private * d_func() { return _d; }
private:
Private * _d;
};
</pre>
<p>Instead of accessing <code>_d</code> in method implementations, one always uses <code>d_func()</code>:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
void Class::f() const {
const Private * d = f_func();
// use 'd' ...
}
</pre>
<p>Of course, nothing prevents the direct use of <code>_d</code> here; something that isn’t possible when using <code>deep_const_ptr</code>. This variant, therefore, requires a little more programmer discipline. In addition, the developer can extend <code>deep_const_ptr</code> such that its destructor deletes the payload for him, while he himself is responsible for deleting <code>_d</code>. In return, the <code>d_func()</code> variant scores points when dealing with polymorphic class hierarchies, as will be shown later.</p>
<p><a name="back-link"></a></p>
<h3>Accessing The Public Class</h3>
<p>A further obstacle arises when a developer actually wishes to move <em>all</em> private functions from the public to the <code>Private</code> class: He is missing a way to call (non-static) public or protected methods of the public class from methods on <code>Private</code>, since the link of the public class to its <code>Private</code> class is <em>unidirectional</em>:</p>
<pre class="brush: cpp; highlight: [5]; title: ; notranslate" title="">
class Class::Private {
public:
Private() : ... {}
// ...
void callPublicFunc() { /*???*/Class::publicFunc(); }
};
Class::Class()
: d( new Private ) {}
</pre>
<p>The problem can be solved by introducing a back-link (the name chosen here, <code>q</code>, originates from Qt):</p>
<pre class="brush: cpp; highlight: [2,4,10]; title: ; notranslate" title="">
class Class::Private {
Class * const q; // back-link
public:
explicit Private( Class * qq ) : q( qq ), ... {}
// ...
void callPublicFunc() { q->publicFunc(); }
};
Class::Class()
: d( new Private( this ) ) {}
</pre>
<p>When using the back-link it is imperative to bear in mind that the initialisation of <code>d</code> is not ensured until the <code>Private</code> constructor has finished executing. One should avoid calling (<code>Class</code>) methods which require a valid <code>d</code> pointer during the execution of the <code>Private</code> constructor, lest it rains crashes or undefined behaviour.</p>
<p>The security-minded developer will therefore initialise the back-links to zero first, and only implant them the reference to the public class after construction is complete:</p>
<pre class="brush: cpp; highlight: [4,9,11,12]; title: ; notranslate" title="">
class Class::Private {
Class * const q; // back-link
public:
explicit Private( /*Class * qq*/ ) : q( 0 ), ... {}
// ...
};
Class::Class()
: d( new Private/*( this )*/ )
{
// establish back-link:
d->q = this;
}
</pre>
<p>Despite these restrictions, a considerable amount of the initalisation code of a class can usually be moved into the <code>Private</code> constructor, which is valuable in classes with overloaded constructors. It should not be left unmentioned that the <code>q</code>-pointer, too, can be talked into propagating constness using the <code>deep_const_ptr</code> or, in case of class hierarchies, <code>q_func()</code>-functions.</p>
<p>Having re-added missing functionality now, the rest of the article will show how to attenuate the Pimpl overhead with a trick from the depths of the magic bag.</p>
<p><a name="recycling"></a></p>
<h3>Raising Efficiency Through Recycling</h3>
<p>As a good C++ programmer, the reader will have become sceptical as he read the introductory remarks on the classical Pimpl idiom in Part One. In particular, the additional dynamic memory allocation will have caused headache, the more so if classes otherwise allocate little or no additional memory.</p>
<p>Even though such impressions should first be verified with a profiler, it cannot hurt acceptance of the technique to find mechanisms that soften the potential performance trap. Part One already mentioned the direct embedding of data fields, with which dynamic memory allocations can be saved. In the following, we will look at an additional, much more involved, technique: the recycling of the <code>d</code>-pointer.</p>
<p>In a polymorphic class hierarchy, the problem of additional dynamic memory allocations caused by Pimpl multiplies by the depth of the hierarchy: Each class in the hierarchy has its own “pimple”, even though for some it might be completely empty (for example, if one only reimplemented virtual functions, but added no additional data members).</p>
<p>The developer can fight the proliferation of <code>d</code>-pointers (and of the dynamic memory allocations associated with them) by re-using the base-class <code>d</code>-pointer in derived classes:</p>
<pre class="brush: cpp; highlight: [6,8,22,23,24,25,26]; title: ; notranslate" title="">
// base.h:
class Base {
// ...
public:
Base();
protected:
class Private;
explicit Base( Private * d );
Private * d_func() { return _d; }
const Private * d_func() const { return _d; }
private:
Private * _d;
};
// base.cpp:
Base::Base()
: _d( new Private )
{
// ...
}
Base::Base( Private * d )
: _d( d )
{
// ...
}
</pre>
<p>The addition of the <code>protected</code> constructor alongside the public ones allows derived classes to implant their own <code>d</code>-pointer into the base class. The code also employs <code>const</code> forwarding using (now <code>protected</code>) <code>d_func()</code>-functions — and not <code>deep_const_ptr</code> — to allow derived classes (read-)access to <code>_d</code>.</p>
<pre class="brush: cpp; title: ; notranslate" title="">
// derived.h:
class Derived : public Base {
public:
Derived();
// ...
protected:
class Private;
Private * d_func(); // can't be implemented inline here
const Private * d_func() const; // ditto
};
// derived.cpp:
Derived::Private * Derived::d_func() {
return static_cast<Private*>( Base::d_func() );
}
const Derived::Private * Derived::d_func() const {
return static_cast<const Private*>( Base::d_func() );
}
Derived::Derived()
: Base( new Private ) {}
</pre>
<p>The author of <code>Derived</code> now uses the newly added <code>Base</code> constructor to implant a <code>Derived::Private</code> instead of a <code>Base::Private</code> into <code>Base::_d</code> (note the resolution of the unqualified name <code>Private</code> in the different contexts). He also implements the <code>Derived::d_func()</code> overloads in terms of the <code>Base::d_func()</code> ones, but returns his own <code>Private</code> class instead.</p>
<p>For the <code>Base</code> constructor call to work, <code>Derived::Private</code> needs to inherit from <code>Base::Private</code>:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
class Derived::Private : public Base::Private {
// ...
};
</pre>
<p>To actually be able to perform this inheritance, three conditions must be met: First, the developer has to declare the <code>Base::Private</code> destructor virtual, otherwise he’ll be caught up in undefined behaviour when the <code>Base</code> destructor deletes the <code>Private</code> class hierarchy.</p>
<p>Furthermore, he must implement both classes in the same library, since the <code>Private</code> classes are usually not exported — they carry no <code>declspec(dllexport)</code> on Windows and are not <code>visibility=hidden</code> in ELF binaries (Executable and Linkable Format). Export would be necessary, however, if <code>Derived</code> lay in a different library than <code>Base</code>. In exceptional cases, the <code>Private</code> classes of central classes may be exported: Nokia engineers, for example, have exported the classes <code>QObjectPrivate</code> (from QtCore) and <code>QWidgetPrivate</code> (from QtGui), which are so central to the Qt library, since so many classes from modules other than QtCore and QtGui derive from <code>QObject</code> and <code>QWidget</code>. Doing so, however, ties such libraries intrinsically to each other at the version level, such that end users can normally exchange them only in conjunction with each other: In general, a <code>libQtGui.so.4.5.0</code> will not work if the runtime environment links it against a <code>libQtCore.so.4.6.0</code>.</p>
<p>Third, the definition of <code>Base::Private</code> can no longer be hidden in the implementation file of the base class (<code>base.cpp</code>), since the definition of <code>Derived::Private</code> requires it. So where to put the <code>Base::Private</code> definition? The developer can hardly put it into the header file (<code>base.h</code>), then he could just do away with the effort to use Pimpl. The answer lies in the creation of a second, private header file. Qt and KDE established the <code>classname_p.h</code> naming scheme for this purpose (<code>_priv</code>, <code>_i</code> and <code>_impl</code> suffixes are also common). Besides the <code>Base::Private</code> definition, the header file may also hold <code>inline</code> definitions of <code>Base</code> methods, for example the new constructor:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
inline Base::Base( Private * d ) : _d( d ) {}
</pre>
<p>And in <code>derived_p.h</code>:</p>
<pre class="brush: cpp; title: ; notranslate" title="">
inline Derived::Derived( Private * d ) : Base( d ) {}
inline const Derived::Private * Derived::d_func() const {
return static_cast<const Private*>( Base::d_func() );
}
inline Derived::Private * Derived::d_func() {
return static_cast<Private*>( Base::d_func() );
}
</pre>
<p>Strictly speaking, the definitions as shown above violate the ODR (One Definition Rule), since the <code>d_func()</code> functions are <code>inline</code> in those translation units which include <code>derived_p.h</code>, but <code>extern</code> in all others.</p>
<p>In practice, however, that is not a problem, since all users of <code>d_func()</code> have to include <code>derived_p.h</code>, too. To be on the safe side, declare <code>Derived::d_func()</code> <code>inline</code> already in the class definition; current compilers don’t mind the missing definition.</p>
<p>In practice, one hides the non-negligible code noise introduced by this technique in preprocessor macros. Qt, for example, has a <code>Q_DECLARE_PRIVATE</code> macro that class definitions can use, as well as <code>Q_D</code>, which declares a local <code>d</code> pointer in method implementations and initialises it with a call to <code>d_func()</code>.</p>
<p>One downside still remains, though: If the developer wants to combine <code>d</code>-pointer recycling with back-links, some complications ensue. To begin with, meticulous attention needs to be paid not to dereference (neither directly nor indirectly) the <code>Derived</code> pointer passed to the <code>Private</code> constructor, until the whole class hierarchy has finished constructing.</p>
<pre class="brush: cpp; title: ; notranslate" title="">
Derived::Private( Derived * qq )
: Base( qq ) // ok, not dereferencing
{
q->setFoo( ... ); // dereferences q -> crash!
}
</pre>
<p>For not only has <code>Derived</code> not finished constructing at the moment of the <code>Private</code> constructor call, neither has — and that is the difference to the non-polymorphic case discussed above — <code>Base</code>: Its constructor hasn’t been entered yet; the constructor argument is still under construction.</p>
<p>In this case, too, it helps to initialise the back-link to <code>0</code> first. The task to set the back-link then falls to the most-derived class, that is, the one that implants its concrete <code>Private</code> object into the hierarchy. In the case of <code>Derived</code>, this would look as follows:</p>
<pre class="brush: cpp; highlight: [4]; title: ; notranslate" title="">
Derived::Derived()
: Base( new Private/*( this )*/ )
{
d_func()->_q = this;
}
</pre>
<p>The author customarily rolls parts of the initialisation that require the back-link into a separate <code>Private::init()</code> function (=two-step construction of <code>Private</code>), called (only) by the constructor whose own <code>Private</code> class is being used.</p>
<pre class="brush: cpp; title: ; notranslate" title="">
Derived::Derived( Private * d )
: Base( d )
{
// does _not_ call d->init()!
}
Derived::Derived()
: Base( new Private )
{
d_func()->init( this );
}
Derived::Private::init( Derived * qq ) {
Base::Private::init( qq ); // sets _q
// my initialisation goes here
}
</pre>
<p>Furthermore, each <code>Private</code> class needs to declare its own back-link, or else “<code>q_func()</code>” methods that take care of casting the base-class back-link. The code needed for this is left as an exercise to you, gentle reader. The solution can be found on the <a href="ftp://ftp.heise.de/pub/ix/developer/pimpl.zip">Heise FTP server</a> in the form of a “pimped” <code>Shape</code>-hierarchy.</p>
<p><a name="findings"></a></p>
<h3>Findings</h3>
<p>As a well-known C++ idiom, Pimpl allows class authors to separate class interface and implementation to an extent not directly provided for by C++. As a positive side-effect, the use of d-pointers speeds up compilation runs, eases implementation of transaction semantics, and allows, through extended means of composition, implementations that potentially are more runtime-efficient.</p>
<p>Not everything is shiny when using d-pointers, though: In addition to the extra <code>Private</code> class, and its dynamic memory allocation, modified <code>const</code> method semantics, as well as potential allocation sequence errors are cause for concern. For both of these, this article has presented remedies, which, however, cause a lot more coding effort. Because of the increased complexity involved in these, the “fully pimped” Pimpl, including recycling and back-links, can be recommended only for a few selected classes or projects.</p>
<p>However, projects that do not shy away from the effort will be rewarded with intriguing interface stability, allowing far-reaching implementation changes.</p>
<h3>Literature</h3>
<ul>
<li>John Lakos; Large-Scale C++ Software Design; Addison-Wesley Longman, 1996</li>
<li>Herb Sutter; Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions; Addison-Wesley Longman, 2000</li>
<li>Herb Sutter, Andrei Alexandrescu: C++ Coding Standards: 101 Rules, Guidelines and Best Practices; Addison-Wesley Longman, 2004</li>
<li>Marc Mutz; Pimp my Pimpl; C++: Vor- und Nachteile des d-Zeiger-Idioms, Teil 1; <a href="http://www.heise.de/developer/artikel/C-Vor-und-Nachteile-des-d-Zeiger-Idioms-Teil-2-1136104.html">Artikel</a> auf heise Developer (<a href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl/" title="Pimp My Pimpl">English Translation available</a>)</ul>
</ul>
<div id="jp-post-flair" class="sharedaddy sd-rating-enabled sd-like-enabled sd-sharing-enabled"><div class="sd-block sd-rating"><h3 class="sd-title">Rate this:</h3><div class="pd-rating" id="pd_rating_holder_2480796_page_306"></div></div><div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing"><h3 class="sd-title">Share this:</h3><div class="sd-content"><ul><li class="share-twitter"><a rel="nofollow" data-shared="sharing-twitter-306" class="share-twitter sd-button share-icon" href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/?share=twitter" target="_blank" title="Click to share on Twitter"><span>Twitter</span></a></li><li class="share-reddit"><a rel="nofollow" data-shared="" class="share-reddit sd-button share-icon" href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/?share=reddit" target="_blank" title="Click to share on Reddit"><span>Reddit</span></a></li><li class="share-google-plus-1"><a rel="nofollow" data-shared="sharing-google-306" class="share-google-plus-1 sd-button share-icon" href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/?share=google-plus-1" target="_blank" title="Click to share on Google+"><span>Google</span></a></li><li class="share-email"><a rel="nofollow" data-shared="" class="share-email sd-button share-icon" href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/?share=email" target="_blank" title="Click to email"><span>Email</span></a></li><li class="share-print"><a rel="nofollow" data-shared="" class="share-print sd-button share-icon" href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/#print" target="_blank" title="Click to print"><span>Print</span></a></li><li class="share-end"></li></ul></div></div></div><div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='like-post-wrapper-14776649-306-5686884c75e78' data-src='//widgets.wp.com/likes/#blog_id=14776649&post_id=306&origin=marcmutz.wordpress.com&obj_id=14776649-306-5686884c75e78' data-name='like-post-frame-14776649-306-5686884c75e78'><h3 class='sd-title'>Like this:</h3><div class='likes-widget-placeholder post-likes-widget-placeholder' style='height:55px'><span class='button'><span>Like</span></span> <span class="loading">Loading...</span></div><span class='sd-text-color'></span><a class='sd-link-color'></a></div></div><div class="clear"></div>
<div class="clear"></div>
</div>
</div>
<div id="comments">
<div id="comments">
<h3 id="comments-title">3 Responses to <em>Pimp My Pimpl — Reloaded</em> </h3>
<ol class="commentlist snap_preview">
<li class="post pingback">
<p>Pingback: <a href='https://marcmutz.wordpress.com/2010/11/15/heise-developer-pimp-my-pimpl-part-2/' rel='external nofollow' class='url'>Heise Developer: Pimp My Pimpl (part 2) « -Wmarc</a></p>
</li><!-- #comment-## -->
<li class="post pingback">
<p>Pingback: <a href='https://marcmutz.wordpress.com/2011/08/14/translated-pimp-my-pimpl-part-2/' rel='external nofollow' class='url'>Translated: Pimp My Pimpl (part 2) « -Wmarc</a></p>
</li><!-- #comment-## -->
<li class="post pingback">
<p>Pingback: <a href='http://techretriever.com/%d0%bf%d0%b5%d1%80%d0%b5%d0%b2%d0%be%d0%b4-%d1%81%d1%82%d0%b0%d1%82%d1%8c%d0%b8-pimp-my-pimpl-%d1%87%d0%b0%d1%81%d1%82%d1%8c-2-c-%d1%85%d0%b0%d0%b1%d1%80%d0%b0%d1%85%d0%b0%d0%' rel='external nofollow' class='url'>Перевод статьи «Pimp my Pimpl», часть 2 / C++ / Хабрахабр | TechRetriever</a></p>
</li><!-- #comment-## -->
</ol>
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title">Leave a Reply <small><a rel="nofollow" id="cancel-comment-reply-link" href="/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/#respond" style="display:none;">Cancel reply</a></small></h3> <form action="https://marcmutz.wordpress.com/wp-comments-post.php" method="post" id="commentform" class="comment-form">
<input type="hidden" id="highlander_comment_nonce" name="highlander_comment_nonce" value="21d3177554" /><input type="hidden" name="_wp_http_referer" value="/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/" />
<input type="hidden" name="hc_post_as" id="hc_post_as" value="guest" />
<div class="comment-form-field comment-textarea">
<label for="comment">Enter your comment here...</label>
<div id="comment-form-comment"><textarea id="comment" name="comment" title="Enter your comment here..."></textarea></div>
</div>
<div id="comment-form-identity">
<div id="comment-form-nascar">
<p>Fill in your details below or click an icon to log in:</p>
<ul>
<li class="selected" style="display:none;">
<a href="#comment-form-guest" id="postas-guest" title="Guest">
<span></span>
</a>
</li>
<li>
<a href="#comment-form-load-service:WordPress.com" id="postas-wordpress" title="WordPress.com">
<span></span>
</a>
</li>
<li>
<a href="#comment-form-load-service:Twitter" id="postas-twitter" title="Twitter">
<span></span>
</a>
</li>
<li>
<a href="#comment-form-load-service:Facebook" id="postas-facebook" title="Facebook">
<span></span>
</a>
</li>
<li>
<iframe id="googleplus-sign-in" name="googleplus-sign-in" src="https://public-api.wordpress.com/connect/?googleplus-sign-in=https%3A%2F%2Fmarcmutz.wordpress.com" width="24" height="24" scrolling="no" allowtransparency="true" seamless="seamless" frameborder="0"></iframe>
</li>
</ul>
</div>
<div id="comment-form-guest" class="comment-form-service selected">
<div class="comment-form-padder">
<div class="comment-form-avatar">
<a href="https://gravatar.com/site/signup/" target="_blank"> <img src="https://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=25&d=identicon&forcedefault=y&r=G" alt="Gravatar" width="25" class="no-grav" />
</a> </div>
<div class="comment-form-fields">
<div class="comment-form-field comment-form-email">
<label for="email">Email <span class="required">(required)</span> <span class="nopublish">(Address never made public)</span></label>
<div class="comment-form-input"><input id="email" name="email" type="email" value="" /></div>
</div>
<div class="comment-form-field comment-form-author">
<label for="author">Name <span class="required">(required)</span></label>
<div class="comment-form-input"><input id="author" name="author" type="text" value="" /></div>
</div>
<div class="comment-form-field comment-form-url">
<label for="url">Website</label>
<div class="comment-form-input"><input id="url" name="url" type="text" value="" /></div>
</div>
</div>
</div>
</div>
<div id="comment-form-wordpress" class="comment-form-service">
<div class="comment-form-padder">
<div class="comment-form-avatar">
<img src="https://s2.wp.com/wp-content/mu-plugins/highlander-comments/images/wplogo.png" alt="WordPress.com Logo" width="25" class="no-grav" />
</div>
<div class="comment-form-fields">
<input type="hidden" name="wp_avatar" id="wordpress-avatar" class="comment-meta-wordpress" value="" />
<input type="hidden" name="wp_user_id" id="wordpress-user_id" class="comment-meta-wordpress" value="" />
<input type="hidden" name="wp_access_token" id="wordpress-access_token" class="comment-meta-wordpress" value="" />
<p class="comment-form-posting-as pa-wordpress"><strong></strong> You are commenting using your WordPress.com account. <span class="comment-form-log-out">( <a href="javascript:HighlanderComments.doExternalLogout( 'wordpress' );">Log Out</a> / <a href="#" onclick="javascript:HighlanderComments.switchAccount();return false;">Change</a> )</span></p>
</div>
</div>
</div>
<div id="comment-form-twitter" class="comment-form-service">
<div class="comment-form-padder">
<div class="comment-form-avatar">
<img src="https://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=25&d=identicon&forcedefault=y&r=G" alt="Twitter picture" width="25" class="no-grav" />
</div>
<div class="comment-form-fields">
<input type="hidden" name="twitter_avatar" id="twitter-avatar" class="comment-meta-twitter" value="" />
<input type="hidden" name="twitter_user_id" id="twitter-user_id" class="comment-meta-twitter" value="" />
<input type="hidden" name="twitter_access_token" id="twitter-access_token" class="comment-meta-twitter" value="" />
<p class="comment-form-posting-as pa-twitter"><strong></strong> You are commenting using your Twitter account. <span class="comment-form-log-out">( <a href="javascript:HighlanderComments.doExternalLogout( 'twitter' );">Log Out</a> / <a href="#" onclick="javascript:HighlanderComments.switchAccount();return false;">Change</a> )</span></p>
</div>
</div>
</div>
<div id="comment-form-facebook" class="comment-form-service">
<div class="comment-form-padder">
<div class="comment-form-avatar">
<img src="https://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=25&d=identicon&forcedefault=y&r=G" alt="Facebook photo" width="25" class="no-grav" />
</div>
<div class="comment-form-fields">
<input type="hidden" name="fb_avatar" id="facebook-avatar" class="comment-meta-facebook" value="" />
<input type="hidden" name="fb_user_id" id="facebook-user_id" class="comment-meta-facebook" value="" />
<input type="hidden" name="fb_access_token" id="facebook-access_token" class="comment-meta-facebook" value="" />
<p class="comment-form-posting-as pa-facebook"><strong></strong> You are commenting using your Facebook account. <span class="comment-form-log-out">( <a href="javascript:HighlanderComments.doExternalLogout( 'facebook' );">Log Out</a> / <a href="#" onclick="javascript:HighlanderComments.switchAccount();return false;">Change</a> )</span></p>
</div>
</div>
</div>
<div id="comment-form-googleplus" class="comment-form-service">
<div class="comment-form-padder">
<div class="comment-form-avatar">
<img src="https://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=25&d=identicon&forcedefault=y&r=G" alt="Google+ photo" width="25" class="no-grav" />
</div>
<div class="comment-form-fields">
<input type="hidden" name="googleplus_avatar" id="googleplus-avatar" class="comment-meta-googleplus" value="" />
<input type="hidden" name="googleplus_user_id" id="googleplus-user_id" class="comment-meta-googleplus" value="" />
<input type="hidden" name="googleplus_access_token" id="googleplus-access_token" class="comment-meta-googleplus" value="" />
<p class="comment-form-posting-as pa-googleplus"><strong></strong> You are commenting using your Google+ account. <span class="comment-form-log-out">( <a href="javascript:HighlanderComments.doExternalLogout( 'googleplus' );">Log Out</a> / <a href="#" onclick="javascript:HighlanderComments.switchAccount();return false;">Change</a> )</span></p>
</div>
</div>
</div>
<div id="comment-form-load-service" class="comment-form-service">
<div class="comment-form-posting-as-cancel"><a href="javascript:HighlanderComments.cancelExternalWindow();">Cancel</a></div>
<p>Connecting to %s</p>
</div>
</div>
<script type="text/javascript">
var highlander_expando_javascript = function(){
var input = document.createElement( 'input' ),
comment = jQuery( '#comment' );
if ( 'placeholder' in input ) {
comment.attr( 'placeholder', jQuery( '.comment-textarea label' ).remove().text() );
}
// Expando Mode: start small, then auto-resize on first click + text length
jQuery( '#comment-form-identity' ).hide();
jQuery( '#comment-form-subscribe' ).hide();
jQuery( '#commentform .form-submit' ).hide();
comment.css( { 'height':'10px' } ).one( 'focus', function() {
var timer = setInterval( HighlanderComments.resizeCallback, 10 )
jQuery( this ).animate( { 'height': HighlanderComments.initialHeight } ).delay( 100 ).queue( function(n) { clearInterval( timer ); HighlanderComments.resizeCallback(); n(); } );
jQuery( '#comment-form-identity' ).slideDown();
jQuery( '#comment-form-subscribe' ).slideDown();
jQuery( '#commentform .form-submit' ).slideDown();
});
}
jQuery(document).ready( highlander_expando_javascript );
</script>
<div id="comment-form-subscribe">
<p class="comment-subscription-form"><input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;" tabindex="6"/> <label class="subscribe-label" id="subscribe-label" for="subscribe" style="display: inline;">Notify me of new comments via email.</label></p><p class="post-subscription-form"><input type="checkbox" name="subscribe_blog" id="subscribe_blog" value="subscribe" style="width: auto;" tabindex="7"/> <label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog" style="display: inline;">Notify me of new posts via email.</label></p></div>
<p class="form-submit"><input name="submit" type="submit" id="comment-submit" class="submit" value="Post Comment" /> <input type='hidden' name='comment_post_ID' value='306' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</p><p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="3707221f3c" /></p>
<input type="hidden" name="genseq" value="1451657292" />
<p style="display: none;"><input type="hidden" id="ak_js" name="ak_js" value="28"/></p> </form>
</div><!-- #respond -->
<div style="clear: both"></div>
</div><!-- #comments --> </div>
</div>
<div id="sidebar">
<div id="top-posts-2" class="widget widget_top-posts"><h4>Top Posts</h4><ul><li><a href="https://marcmutz.wordpress.com/effective-qt/containers/" class="bump-view" data-bump-view="tp">Understand the Qt containers</a></li>
<li><a href="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl/" class="bump-view" data-bump-view="tp">Pimp My Pimpl</a></li>
<li><a href="https://marcmutz.wordpress.com/effective-qt/subclassing/" class="bump-view" data-bump-view="tp">Don't be sub-class when subclassing</a></li>
<li><a href="https://marcmutz.wordpress.com/private-practice/whats-in-a-proxy-style/" class="bump-view" data-bump-view="tp">What's in a Proxy-Style?</a></li>
<li><a href="https://marcmutz.wordpress.com/2010/10/14/top-5-reasons-you-should-love-your-ternary-operator/" class="bump-view" data-bump-view="tp">Top 5 reasons you should love your ternary operator</a></li>
</ul></div><div id="tag_cloud-2" class="widget widget_tag_cloud"><h4></h4><div style="overflow: hidden;"><a href="https://marcmutz.wordpress.com/category/projects/akonadi/" style="font-size: 100%; padding: 1px; margin: 1px;" title="Akonadi (1)">Akonadi</a> <a href="https://marcmutz.wordpress.com/category/software-libraries/boost/" style="font-size: 135%; padding: 1px; margin: 1px;" title="Boost (6)">Boost</a> <a href="https://marcmutz.wordpress.com/category/programming-languages/c/" style="font-size: 268%; padding: 1px; margin: 1px;" title="C++ (25)">C++</a> <a href="https://marcmutz.wordpress.com/category/programming-languages/c/c0x/" style="font-size: 170%; padding: 1px; margin: 1px;" title="C++0x (11)">C++0x</a> <a href="https://marcmutz.wordpress.com/category/columns/effective-qt/" style="font-size: 128%; padding: 1px; margin: 1px;" title="Effective Qt (5)">Effective Qt</a> <a href="https://marcmutz.wordpress.com/category/english/" style="font-size: 275%; padding: 1px; margin: 1px;" title="English (26)">English</a> <a href="https://marcmutz.wordpress.com/category/german/" style="font-size: 198%; padding: 1px; margin: 1px;" title="German (15)">German</a> <a href="https://marcmutz.wordpress.com/category/projects/gpg4win/" style="font-size: 121%; padding: 1px; margin: 1px;" title="Gpg4win (4)">Gpg4win</a> <a href="https://marcmutz.wordpress.com/category/columns/heise-developer/" style="font-size: 149%; padding: 1px; margin: 1px;" title="Heise Developer (8)">Heise Developer</a> <a href="https://marcmutz.wordpress.com/category/columns/private-practice/" style="font-size: 114%; padding: 1px; margin: 1px;" title="Private Practice (3)">Private Practice</a> <a href="https://marcmutz.wordpress.com/category/software-libraries/qt/" style="font-size: 205%; padding: 1px; margin: 1px;" title="Qt (16)">Qt</a> </div></div><div id="image-3" class="widget widget_image"><h4>StackOverflow</h4><div style="overflow:hidden;"><a href="http://stackoverflow.com/users/134841/mmutz"><img src="http://stackoverflow.com/users/flair/134841.png?theme=clean" alt="profile for mmutz at Stack Overflow, Q&A for professional and enthusiast programmers" title="profile for mmutz at Stack Overflow, Q&A for professional and enthusiast programmers" class="alignright" width="208" height="58" /></a></div>
</div><div id="archives-3" class="widget widget_archive"><h4>Monthly</h4> <ul>
<li><a href='https://marcmutz.wordpress.com/2012/02/'>February 2012</a> (1)</li>
<li><a href='https://marcmutz.wordpress.com/2012/01/'>January 2012</a> (1)</li>
<li><a href='https://marcmutz.wordpress.com/2011/11/'>November 2011</a> (3)</li>
<li><a href='https://marcmutz.wordpress.com/2011/10/'>October 2011</a> (2)</li>
<li><a href='https://marcmutz.wordpress.com/2011/09/'>September 2011</a> (3)</li>
<li><a href='https://marcmutz.wordpress.com/2011/08/'>August 2011</a> (3)</li>
<li><a href='https://marcmutz.wordpress.com/2011/07/'>July 2011</a> (2)</li>
<li><a href='https://marcmutz.wordpress.com/2011/04/'>April 2011</a> (2)</li>
<li><a href='https://marcmutz.wordpress.com/2011/03/'>March 2011</a> (1)</li>
<li><a href='https://marcmutz.wordpress.com/2010/12/'>December 2010</a> (1)</li>
<li><a href='https://marcmutz.wordpress.com/2010/11/'>November 2010</a> (1)</li>
<li><a href='https://marcmutz.wordpress.com/2010/10/'>October 2010</a> (2)</li>
<li><a href='https://marcmutz.wordpress.com/2010/09/'>September 2010</a> (1)</li>
<li><a href='https://marcmutz.wordpress.com/2010/08/'>August 2010</a> (3)</li>
<li><a href='https://marcmutz.wordpress.com/2010/07/'>July 2010</a> (7)</li>
</ul>
</div><div id="linkcat-1356" class="widget widget_links"><h4>Blogroll</h4>
<ul class='xoxo blogroll'>
<li><a href="http://steveire.wordpress.com/">Stephen's Blog</a></li>
<li><a href="http://ervin.ipsquad.net/">Kevin's Blog</a></li>
<li><a href="http://c2143.blogspot.com/">Romain's Blog</a></li>
<li><a href="http://blogs.kde.org/blog/326">David's Blog</a></li>
<li><a href="http://thomasmcguire.wordpress.com/">Thomas' Blog</a></li>
</ul>
</div>
</div>
</div>
<div id="footer">
<div class="footerleft">
<p><a href="https://wordpress.com/?ref=footer_website">Create a free website or blog at WordPress.com.</a></p>
</div>
<div class="footerright">
<a href="https://wordpress.com/themes/enterprise/" title="Learn more about this theme">The Enterprise Theme</a>. </div>
</div>
</div>
<!-- wpcom_wp_footer -->
<script type='text/javascript' src='//0.gravatar.com/js/gprofiles.js?ver=201653y'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var WPGroHo = {"my_hash":""};
/* ]]> */
</script>
<script type='text/javascript' src='https://s2.wp.com/wp-content/mu-plugins/gravatar-hovercards/wpgroho.js?m=1380573781g'></script>
<script>
//initialize and attach hovercards to all gravatars
jQuery( document ).ready( function( $ ) {
if (typeof Gravatar === "undefined"){
return;
}
if ( typeof Gravatar.init !== "function" ) {
return;
}
Gravatar.profile_cb = function( hash, id ) {
WPGroHo.syncProfileData( hash, id );
};
Gravatar.my_hash = WPGroHo.my_hash;
Gravatar.init( 'body', '#wp-admin-bar-my-account' );
});
</script>
<div style="display:none">
</div>
<script type='text/javascript'>
/* <![CDATA[ */
var HighlanderComments = {"loggingInText":"Logging In\u2026","submittingText":"Posting Comment\u2026","postCommentText":"Post Comment","connectingToText":"Connecting to %s","commentingAsText":"%1$s: You are commenting using your %2$s account.","logoutText":"Log Out","loginText":"Log In","connectURL":"https:\/\/marcmutz.wordpress.com\/public.api\/connect\/?action=request","logoutURL":"https:\/\/marcmutz.wordpress.com\/wp-login.php?action=logout&_wpnonce=12973c276c","homeURL":"https:\/\/marcmutz.wordpress.com\/","postID":"306","gravDefault":"identicon","enterACommentError":"Please enter a comment","enterEmailError":"Please enter your email address here","invalidEmailError":"Invalid email address","enterAuthorError":"Please enter your name here","gravatarFromEmail":"This picture will show whenever you leave a comment. Click to customize it.","logInToExternalAccount":"Log in to use details from one of these accounts.","change":"Change","changeAccount":"Change Account","comment_registration":"","userIsLoggedIn":"","isJetpack":"0","text_direction":"ltr"};
/* ]]> */
</script>
<script type='text/javascript' src='https://s2.wp.com/_static/??/wp-content/js/jquery/jquery.autoresize.js,/wp-content/mu-plugins/highlander-comments/script.js?m=1424115551j'></script>
<div id="bit" class="loggedout-follow-normal">
<a class="bsub" href="javascript:void(0)"><span id='bsub-text'>Follow</span></a>
<div id="bitsubscribe">
<h3><label for="loggedout-follow-field">Follow “-Wmarc”</label></h3>
<form action="https://subscribe.wordpress.com" method="post" accept-charset="utf-8" id="loggedout-follow">
<p>Get every new post delivered to your Inbox.</p>
<p id="loggedout-follow-error" style="display: none;"></p>
<p class="bit-follow-count">Join 26 other followers</p>
<p><input type="email" name="email" placeholder="Enter your email address" id="loggedout-follow-field"/></p>
<input type="hidden" name="action" value="subscribe"/>
<input type="hidden" name="blog_id" value="14776649"/>
<input type="hidden" name="source" value="https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/"/>
<input type="hidden" name="sub-type" value="loggedout-follow"/>
<input type="hidden" id="_wpnonce" name="_wpnonce" value="52fddace73" /><input type="hidden" name="_wp_http_referer" value="/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/" />
<p id='bsub-subscribe-button'><input type="submit" value="Sign me up" /></p>
</form>
<div id='bsub-credit'><a href="https://wordpress.com/?ref=lof">Build a website with WordPress.com</a></div>
</div><!-- #bitsubscribe -->
</div><!-- #bit -->
<script type='text/javascript' charset='UTF-8' id='polldaddyRatings'><!--//--><![CDATA[//><!--
PDRTJS_settings_2480796_page_306={"id":2480796,"unique_id":"wp-page-306","title":"Pimp%20My%20Pimpl%20%E2%80%94%26nbsp%3BReloaded","permalink":"https:\/\/marcmutz.wordpress.com\/translated-articles\/pimp-my-pimpl-%e2%80%94-reloaded\/","item_id":"_page_306"}; if ( typeof PDRTJS_RATING !== 'undefined' ){if ( typeof PDRTJS_2480796_page_306 == 'undefined' ){PDRTJS_2480796_page_306 = new PDRTJS_RATING( PDRTJS_settings_2480796_page_306 );}}
//--><!]]></script><script type='text/javascript' charset='UTF-8' src='https://polldaddy.com/js/rating/rating.js'></script>
<script type="text/javascript">
window.WPCOM_sharing_counts = {"https:\/\/marcmutz.wordpress.com\/translated-articles\/pimp-my-pimpl-%e2%80%94-reloaded\/":306};
</script>
<script type="text/javascript">
var windowOpen;
jQuery(document).on( 'ready post-load', function(){
jQuery( 'a.share-twitter' ).on( 'click', function() {
if ( 'undefined' !== typeof windowOpen ){ // If there's another sharing window open, close it.
windowOpen.close();
}
windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomtwitter', 'menubar=1,resizable=1,width=600,height=350' );
return false;
});
});
</script>
<script type="text/javascript">
var windowOpen;
jQuery(document).on( 'ready post-load', function(){
jQuery( 'a.share-google-plus-1' ).on( 'click', function() {
if ( 'undefined' !== typeof windowOpen ){ // If there's another sharing window open, close it.
windowOpen.close();
}
windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomgoogle-plus-1', 'menubar=1,resizable=1,width=480,height=550' );
return false;
});
});
</script>
<div id="sharing_email" style="display: none;">
<form action="/translated-articles/pimp-my-pimpl-%e2%80%94-reloaded/" method="post">
<label for="target_email">Send to Email Address</label>
<input type="email" name="target_email" id="target_email" value="" />
<label for="source_name">Your Name</label>
<input type="text" name="source_name" id="source_name" value="" />
<label for="source_email">Your Email Address</label>
<input type="email" name="source_email" id="source_email" value="" />
<input type="text" id="jetpack-source_f_name" name="source_f_name" class="input" value="" size="25" autocomplete="off" />
<script> document.getElementById('jetpack-source_f_name').value = ''; </script>
<div class="recaptcha" id="sharing_recaptcha"></div><input type="hidden" name="recaptcha_public_key" id="recaptcha_public_key" value="6LcYW8MSAAAAADBAuEH9yaPcF7lWh11Iq62ZKtoo" />
<img style="float: right; display: none" class="loading" src="https://s2.wp.com/wp-content/mu-plugins/post-flair/sharing/images/loading.gif" alt="loading" width="16" height="16" />
<input type="submit" value="Send Email" class="sharing_send" />
<a rel="nofollow" href="#cancel" class="sharing_cancel">Cancel</a>
<div class="errors errors-1" style="display: none;">
Post was not sent - check your email addresses! </div>
<div class="errors errors-2" style="display: none;">
Email check failed, please try again </div>
<div class="errors errors-3" style="display: none;">
Sorry, your blog cannot share posts by email. </div>
</form>
</div>
<iframe src='https://widgets.wp.com/likes/master.html?ver=20151208#ver=20151208&mp6=1' scrolling='no' id='likes-master' name='likes-master' style='display:none;'></iframe>
<div id='likes-other-gravatars'><div class="likes-text"><span>%d</span> bloggers like this:</div><ul class="wpl-avatars sd-like-gravatars"></ul></div>
<script type="text/javascript">
//<![CDATA[
var jetpackLikesWidgetQueue = [];
var jetpackLikesWidgetBatch = [];
var jetpackLikesMasterReady = false;
function JetpackLikespostMessage( message, target ) {
if ( "string" === typeof message ){
try{
message = JSON.parse( message );
}
catch(e) {
return;
}
}
pm( {
target: target,
type: 'likesMessage',
data: message,
origin: '*'
} );
}
function JetpackLikesBatchHandler() {
var requests = [];
jQuery( 'div.jetpack-likes-widget-unloaded' ).each( function( i ) {
if ( jetpackLikesWidgetBatch.indexOf( this.id ) > -1 )
return;
jetpackLikesWidgetBatch.push( this.id );
var regex = /like-(post|comment)-wrapper-(\d+)-(\d+)-(\w+)/;
var match = regex.exec( this.id );
if ( ! match || match.length != 5 )
return;
var info = {
blog_id: match[2],
width: this.width
};
if ( 'post' == match[1] ) {
info.post_id = match[3];
} else if ( 'comment' == match[1] ) {
info.comment_id = match[3];
}
info.obj_id = match[4];
requests.push( info );
});
if ( requests.length > 0 ) {
JetpackLikespostMessage( { event: 'initialBatch', requests: requests }, window.frames['likes-master'] );
}
}
function JetpackLikesMessageListener( event ) {
if ( "undefined" == typeof event.event )
return;
if ( 'masterReady' == event.event ) {
jQuery( document ).ready( function() {
jetpackLikesMasterReady = true;
var stylesData = {
event: 'injectStyles'
};
if ( jQuery( 'iframe.admin-bar-likes-widget' ).length > 0 ) {
JetpackLikespostMessage( { event: 'adminBarEnabled' }, window.frames[ 'likes-master' ] );
stylesData.adminBarStyles = {
background: jQuery( '#wpadminbar .quicklinks li#wp-admin-bar-wpl-like > a' ).css( 'background' ),
isRtl: ( 'rtl' == jQuery( '#wpadminbar' ).css( 'direction' ) )
};
}
if ( !window.addEventListener )
jQuery( '#wp-admin-bar-admin-bar-likes-widget' ).hide();
stylesData.textStyles = {
color: jQuery( '.sd-text-color').css( 'color' ),
fontFamily: jQuery( '.sd-text-color' ).css( 'font-family' ),
fontSize: jQuery( '.sd-text-color' ).css( 'font-size' ),
direction: jQuery( '.sd-text-color' ).css( 'direction' ),
fontWeight: jQuery( '.sd-text-color' ).css( 'font-weight' ),
fontStyle: jQuery( '.sd-text-color' ).css( 'font-style' ),
textDecoration: jQuery( '.sd-text-color' ).css('text-decoration')
};
stylesData.linkStyles = {
color: jQuery( '.sd-link-color' ).css('color'),
fontFamily: jQuery( '.sd-link-color' ).css('font-family'),
fontSize: jQuery( '.sd-link-color' ).css('font-size'),
textDecoration: jQuery( '.sd-link-color' ).css('text-decoration'),
fontWeight: jQuery( '.sd-link-color' ).css( 'font-weight' ),
fontStyle: jQuery( '.sd-link-color' ).css( 'font-style' )
};
JetpackLikespostMessage( stylesData, window.frames[ 'likes-master' ] );
JetpackLikesBatchHandler();
jQuery( document ).on( 'inview', 'div.jetpack-likes-widget-unloaded', function() {
jetpackLikesWidgetQueue.push( this.id );
});
});
}
if ( 'showLikeWidget' == event.event ) {
jQuery( '#' + event.id + ' .post-likes-widget-placeholder' ).fadeOut( 'fast', function() {
jQuery( '#' + event.id + ' .post-likes-widget' ).fadeIn( 'fast', function() {
JetpackLikespostMessage( { event: 'likeWidgetDisplayed', blog_id: event.blog_id, post_id: event.post_id, obj_id: event.obj_id }, window.frames['likes-master'] );
});
});
}
if ( 'clickReblogFlair' == event.event ) {
wpcom_reblog.toggle_reblog_box_flair( event.obj_id );
}
if ( 'showOtherGravatars' == event.event ) {
var $container = jQuery( '#likes-other-gravatars' );
var $list = $container.find( 'ul' );
$container.hide();
$list.html( '' );
$container.find( '.likes-text span' ).text( event.total );
jQuery.each( event.likers, function( i, liker ) {
$list.append( '<li class="' + liker.css_class + '"><a href="' + liker.profile_URL + '" class="wpl-liker" rel="nofollow" target="_parent"><img src="' + liker.avatar_URL + '" alt="' + liker.name + '" width="30" height="30" style="padding-right: 3px;" /></a></li>');
} );
var offset = jQuery( "[name='" + event.parent + "']" ).offset();
$container.css( 'left', offset.left + event.position.left - 10 + 'px' );
$container.css( 'top', offset.top + event.position.top - 33 + 'px' );
var rowLength = Math.floor( event.width / 37 );
var height = ( Math.ceil( event.likers.length / rowLength ) * 37 ) + 13;
if ( height > 204 ) {
height = 204;
}
$container.css( 'height', height + 'px' );
$container.css( 'width', rowLength * 37 - 7 + 'px' );
$list.css( 'width', rowLength * 37 + 'px' );
$container.fadeIn( 'slow' );
var scrollbarWidth = $list[0].offsetWidth - $list[0].clientWidth;
if ( scrollbarWidth > 0 ) {
$container.width( $container.width() + scrollbarWidth );
$list.width( $list.width() + scrollbarWidth );
}
}
}
pm.bind( 'likesMessage', function(e) { JetpackLikesMessageListener(e); } );
jQuery( document ).click( function( e ) {
var $container = jQuery( '#likes-other-gravatars' );
if ( $container.has( e.target ).length === 0 ) {
$container.fadeOut( 'slow' );
}
});
function JetpackLikesWidgetQueueHandler() {
var wrapperID;
if ( ! jetpackLikesMasterReady ) {
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
if ( jetpackLikesWidgetQueue.length > 0 ) {
// We may have a widget that needs creating now
var found = false;
while( jetpackLikesWidgetQueue.length > 0 ) {
// Grab the first member of the queue that isn't already loading.
wrapperID = jetpackLikesWidgetQueue.splice( 0, 1 )[0];
if ( jQuery( '#' + wrapperID ).hasClass( 'jetpack-likes-widget-unloaded' ) ) {
found = true;
break;
}
}
if ( ! found ) {
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
} else if ( jQuery( 'div.jetpack-likes-widget-unloaded' ).length > 0 ) {
// Grab any unloaded widgets for a batch request
JetpackLikesBatchHandler();
// Get the next unloaded widget
wrapperID = jQuery( 'div.jetpack-likes-widget-unloaded' ).first()[0].id;
if ( ! wrapperID ) {
// Everything is currently loaded
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
}
if ( 'undefined' === typeof wrapperID ) {
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
var $wrapper = jQuery( '#' + wrapperID );