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
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<doc>
<title>Metaprogramming</title>
<!-- ************************************************************************* -->
<body>
<br/><br/>
<p>
This page documents library components that provide metaprogramming sorts of functionality. For
the most part they are useful for putting design by contract checks into code or doing various kinds of
clever things with templates.
</p>
<p>
For example, you might have a templated function that is templated on a type T and you want to
make sure that T is either a char or wchar_t type. You could place the following into your code
and it would cause the compile to error out when T was set to something other than char or wchar_t.
<br/>
<tt>COMPILE_TIME_ASSERT((is_same_type<T,char>::value || is_same_type<T,wchar_t>::value));</tt>
</p>
</body>
<!-- ************************************************************************* -->
<menu width="200">
<top>
<section>
<name>Objects</name>
<item>is_pointer_type</item>
<item>is_const_type</item>
<item>is_reference_type</item>
<item>is_same_type</item>
<item>is_float_type</item>
<item>is_convertible</item>
<item>is_complex</item>
<item>is_function</item>
<item>is_signed_type</item>
<item>is_unsigned_type</item>
<item>static_switch</item>
<item>noncopyable</item>
<item>enable_if</item>
<item>is_array2d</item>
<item>is_graph</item>
<item>is_rand</item>
<item>is_matrix</item>
<item>is_std_vector</item>
<item>is_pair</item>
<item>is_directed_graph</item>
<item>is_built_in_scalar_type</item>
<item>promote</item>
<item>basic_type</item>
<item>unsigned_type</item>
<item>tabs</item>
<item>tmin</item>
<item>tmax</item>
</section>
<section>
<name>Global Functions</name>
<item>DLIB_ASSERT</item>
<item>DLIB_STACK_TRACE</item>
<item>DLIB_STACK_TRACE_NAMED</item>
<item>get_stack_trace</item>
<item>DLIB_CASSERT</item>
<item>COMPILE_TIME_ASSERT</item>
<item>ASSERT_ARE_SAME_TYPE</item>
<item>DLIB_ASSERT_HAS_STANDARD_LAYOUT</item>
<item>ASSERT_ARE_NOT_SAME_TYPE</item>
<item>_dT</item>
<item>TIME_THIS</item>
<item>is_same_object</item>
<item>assign_zero_if_built_in_scalar_type</item>
<item>wrap_function</item>
</section>
<section>
<name>Other</name>
<item>portability_macros</item>
</section>
</top>
</menu>
<!-- ************************************************************************* -->
<!-- ************************************************************************* -->
<!-- ************************************************************************* -->
<components>
<!-- ************************************************************************* -->
<component>
<name>tmin</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template to compute the min of two values at compile time.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>tmax</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template to compute the max of two values at compile time.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>tabs</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template to compute the absolute value a number at compile time.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>unsigned_type</name>
<file>dlib/uintn.h</file>
<spec_file link="true">dlib/uintn.h</spec_file>
<description>
This is a template that allows you to obtain the unsigned version
of any integral type. For example, unsigned_type<signed short>::type ==
unsigned short.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>static_switch</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
To use this template you give it some number of boolean expressions and it
tells you which one of them is true. If more than one of them is true then
it causes a compile time error. It is useful for cases where you want to
specialize a template and you want to specialize it not by
the type of object it gets per say but instead according to the values of some
type traits associated with the various template arguments. A simple example of
this can be seen in the <a href="imaging.html#assign_pixel">assign_pixel</a>'s
implementation which can be found at the bottom of the <a href="dlib/pixel.h.html">
dlib/pixel.h</a> file.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>enable_if</name>
<file>dlib/enable_if.h</file>
<description>
This is a family of templates from the Boost C++ libraries that makes it somewhat easier to control
template specialization. For the details see <a href="enable_if.html">
this page</a>. Note that the header <tt>dlib/enable_if.h</tt> brings
these templates into the dlib namespace.<br/>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>noncopyable</name>
<file>dlib/noncopyable.h</file>
<spec_file link="true">dlib/noncopyable.h</spec_file>
<description>
This is a simple class that makes it easy to declare a non-copyable object.
To use it to make your own class non-copyable just inherit from it.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_convertible</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template that can be used to determine if one type is convertible
into another type.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_complex</name>
<file>dlib/matrix.h</file>
<spec_file link="true">dlib/matrix/matrix_utilities.h</spec_file>
<description>
This is a template that can be used to determine if a type is a
specialization of std::complex.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_same_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_same_type<T,U>::value == true when T and U are
the same type and false otherwise.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_float_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_float_type<T>::value == true when T is
a floating point type (i.e. float, double, or long double) and false otherwise.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_same_object</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a templated function which checks if both of its arguments are actually
references to the same object. It returns true if they are and false otherwise.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_function</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_function<T>::value == true when T is
a function type.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_signed_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_signed_type<T>::value == true when T is
a signed integral type and false when it is an unsigned integral
type.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_unsigned_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_unsigned_type<T>::value == true when T is
an unsigned integral type and false when it is a signed integral
type.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_directed_graph</name>
<file>dlib/is_kind.h</file>
<spec_file link="true">dlib/is_kind.h</spec_file>
<description>
This is a template where is_directed_graph<T>::value == true when T
is a <a href="containers.html#directed_graph">directed_graph</a> object.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_built_in_scalar_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_built_in_scalar_type<T>::value == true when T
is a built in scalar type such as int, char, float, etc.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>promote</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template that takes one of the built in scalar types and gives you another
scalar type that should be big enough to hold sums of values from the original scalar
type. The new scalar type will also always be signed.
<p>
For example, promote<uint16>::type == int32
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>basic_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template that takes a type and strips off any const, volatile, or reference
qualifiers and gives you back the basic underlying type.
<p>
For example, promote<const int&>::type == int
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_std_vector</name>
<file>dlib/is_kind.h</file>
<spec_file link="true">dlib/is_kind.h</spec_file>
<description>
This is a template where is_std_vector<T>::value == true when T
is a <a href="containers.html#std_vector_c">std_vector_c</a> or std::vector object.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_pair</name>
<file>dlib/is_kind.h</file>
<spec_file link="true">dlib/is_kind.h</spec_file>
<description>
This is a template where is_pair<T>::value == true when T
is a std::pair object.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_matrix</name>
<file>dlib/is_kind.h</file>
<spec_file link="true">dlib/is_kind.h</spec_file>
<description>
This is a template where is_matrix<T>::value == true when T
is a <a href="containers.html#matrix">matrix</a> object or some kind
of matrix expression.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_graph</name>
<file>dlib/is_kind.h</file>
<spec_file link="true">dlib/is_kind.h</spec_file>
<description>
This is a template where is_graph<T>::value == true when T
is a <a href="containers.html#graph">graph</a> object.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_array2d</name>
<file>dlib/is_kind.h</file>
<spec_file link="true">dlib/is_kind.h</spec_file>
<description>
This is a template where is_array2d<T>::value == true when T
is an <a href="containers.html#array2d">array2d</a> object.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_rand</name>
<file>dlib/is_kind.h</file>
<spec_file link="true">dlib/is_kind.h</spec_file>
<description>
This is a template where is_rand<T>::value == true when T
is a <a href="algorithms.html#rand">rand</a> object.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_reference_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_reference_type<T>::value == true when T is a reference
type and false otherwise.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_const_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_const_type<T>::value == true when T is a const
type and false otherwise.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>is_pointer_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template where is_pointer_type<T>::value == true when T is a pointer
type and false otherwise.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>ASSERT_ARE_NOT_SAME_TYPE</name>
<file>dlib/assert.h</file>
<spec_file>dlib/assert.h</spec_file>
<description>
<p>
This is a macro function for debugging. Its form is <tt>ASSERT_ARE_NOT_SAME_TYPE(type1, type2)</tt>.
If type1 and type2 are the same type then the compile will fail. This is sometimes useful
in validating template arguments.
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>ASSERT_ARE_SAME_TYPE</name>
<file>dlib/assert.h</file>
<spec_file>dlib/assert.h</spec_file>
<description>
<p>
This is a macro function for debugging. Its form is <tt>ASSERT_ARE_SAME_TYPE(type1, type2)</tt>.
If type1 and type2 are not the same type then the compile will fail. This is sometimes useful
in validating template arguments.
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>DLIB_ASSERT_HAS_STANDARD_LAYOUT</name>
<file>dlib/assert.h</file>
<spec_file link="true">dlib/assert.h</spec_file>
<description>
<p>
This macro is meant to cause a compiler error if a type doesn't have a simple
memory layout (like a C struct). In particular, types with simple layouts are
ones which can be copied via memcpy().
</p>
This was called a POD type in C++03 and in C++0x we are looking to check if
it is a "standard layout type". Once we can use C++0x we can change this macro
to something that uses the std::is_standard_layout type_traits class.
See: http://www2.research.att.com/~bs/C++0xFAQ.html#PODs
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>COMPILE_TIME_ASSERT</name>
<file>dlib/assert.h</file>
<spec_file>dlib/assert.h</spec_file>
<description>
<p>
This is a macro function for debugging. Its form is <tt>COMPILE_TIME_ASSERT(condition that should
be true)</tt>. The condition must be a compile time constant and if it is false then the compile
will fail.
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>DLIB_CASSERT</name>
<file>dlib/assert.h</file>
<spec_file>dlib/assert.h</spec_file>
<description>
<p>
This is a macro function that is identical to the <a href="#DLIB_ASSERT">DLIB_ASSERT</a> macro
except that it is always enabled. Even if _DEBUG, DEBUG and ENABLE_ASSERTS are not defined.
</p>
<p>
Note that when this macro fails and throws an exception it also calls the global
C function dlib_assert_breakpoint(). This behavior makes it easy to set a debugging
tool to break when DLIB_CASSERT fails by setting a breakpoint on dlib_assert_breakpoint().
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>DLIB_ASSERT</name>
<file>dlib/assert.h</file>
<spec_file>dlib/assert.h</spec_file>
<description>
<p>
This is a macro function for debugging. Its form is <tt>DLIB_ASSERT(condition that should be
true,error message)</tt>. If the condition is false DLIB_ASSERT throws an exception of type
dlib::fatal_error with fatal_error::type == EBROKEN_ASSERT. An error message detailing
the nature of the problem is stored in the member variable info which is of type std::string.
Look in the following file for more details. The exception classes are defined
<a href="dlib/error.h.html#error">here</a>.
</p>
<p>
This macro is only enabled if _DEBUG, DEBUG or ENABLE_ASSERTS is defined. Also, if this macro <i>is</i>
enabled then ENABLE_ASSERTS will be defined even if you didn't define it.
</p>
<p>
Note that when this macro fails and throws an exception it also calls the global
C function dlib_assert_breakpoint(). This behavior makes it easy to set a debugging
tool to break when DLIB_ASSERT fails by setting a breakpoint on dlib_assert_breakpoint().
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>DLIB_STACK_TRACE</name>
<file>dlib/assert.h</file>
<spec_file>dlib/stack_trace.h</spec_file>
<description>
<p>
This is a preprocessor macro that allows you to tag a function so
that dlib will keep track of it in a function call stack. That is,
you will be able to see a stack trace by calling <a href="#get_stack_trace">get_stack_trace</a>
if you put this macro at the top of your functions.
</p>
<p>
This macro is only enabled if DLIB_ENABLE_STACK_TRACE is defined. If it isn't defined then
this macro doesn't do anything. Also note that when this macro is defined it will
cause <a href="#DLIB_ASSERT">DLIB_ASSERT</a> and <a href="#DLIB_CASSERT">DLIB_CASSERT</a>
to include a stack trace in their error messages.
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>DLIB_STACK_TRACE_NAMED</name>
<file>dlib/assert.h</file>
<spec_file>dlib/stack_trace.h</spec_file>
<description>
This is a preprocessor macro just like <a href="#DLIB_STACK_TRACE">DLIB_STACK_TRACE</a>
except that it allows you to supply your own string to use as the function name
in the stack trace instead of the one deduced by DLIB_STACK_TRACE.
<p>
This macro is only enabled if DLIB_ENABLE_STACK_TRACE is defined.
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>get_stack_trace</name>
<file>dlib/assert.h</file>
<spec_file>dlib/stack_trace.h</spec_file>
<description>
This function allows you to query the current stack trace.
<p>
This macro is only enabled if DLIB_ENABLE_STACK_TRACE is defined.
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>_dT</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a macro function for converting a string/character literal to either a char or wchar_t literal.
Its form is <tt>_dT(target character type,string or character literal)</tt>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>assign_zero_if_built_in_scalar_type</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
<p>
This function assigns its argument the value of 0 if it is a built in scalar
type according to the <a href="#is_built_in_scalar_type">is_built_in_scalar_type</a>
template. If it isn't a built in scalar type then it does nothing.
</p>
<p>
This function is useful for suppressing compiler warnings about uninitialized
types inside of templates that are designed to accept the built in types
as well as user defined classes.
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>wrap_function</name>
<file>dlib/algs.h</file>
<spec_file link="true">dlib/algs.h</spec_file>
<description>
This is a template that allows you to turn a global function into a
function object. See the specs for more details.
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>TIME_THIS</name>
<file>dlib/time_this.h</file>
<spec_file>dlib/time_this.h</spec_file>
<description>
<p>
This is a macro function for timing blocks of code. Its form is <tt>TIME_THIS(whatever you want to time)</tt>
It's pretty straight forward. It just prints the time it took to std::cout.
</p>
<p>
There is another version of this function called TIME_THIS_TO which takes as a parameter an ostream
object to write its output to. Its form is <tt>TIME_THIS_TO(what you want to time, the output stream)</tt>;
</p>
</description>
</component>
<!-- ************************************************************************* -->
<component>
<name>portability_macros</name>
<file>dlib/platform.h</file>
<spec_file>dlib/platform.h</spec_file>
<description>
This file #defines various macros depending on the platform being compiled under.
See the file itself for the specifics.
</description>
</component>
<!-- ************************************************************************* -->
</components>
<!-- ************************************************************************* -->
</doc>