aboutsummaryrefslogtreecommitdiff
path: root/Doc/Manual/Cpp0x.html
blob: ef64fecc1b71293b7b5a5bee9a55e0add7fbaca7 (plain)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>SWIG and C++0x</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body bgcolor="#ffffff">
<H1><a name="Cpp0x"></a>7 SWIG and C++0x</H1>
<!-- INDEX -->
<div class="sectiontoc">
<ul>
<li><a href="#Cpp0x_Introduction">Introduction</a>
<li><a href="#Cpp0x_Core_language_changes">Core language changes</a>
<ul>
<li><a href="#Cpp0x_Rvalue_reference_and_move_semantics">Rvalue reference and move semantics</a>
<li><a href="#Cpp0x_Generalized_constant_expressions">Generalized constant expressions</a>
<li><a href="#Cpp0x_Extern_template">Extern template</a>
<li><a href="#Cpp0x_Initializer_lists">Initializer lists</a>
<li><a href="#Cpp0x_Uniform_initialization">Uniform initialization</a>
<li><a href="#Cpp0x_Type_inference">Type inference</a>
<li><a href="#Cpp0x_Range_based_for_loop">Range-based for-loop</a>
<li><a href="#Cpp0x_Lambda_functions_and_expressions">Lambda functions and expressions</a>
<li><a href="#Cpp0x_Alternate_function_syntax">Alternate function syntax</a>
<li><a href="#Cpp0x_Object_construction_improvement">Object construction improvement</a>
<li><a href="#Cpp0x_Null_pointer_constant">Null pointer constant</a>
<li><a href="#Cpp0x_Strongly_typed_enumerations">Strongly typed enumerations</a>
<li><a href="#Cpp0x_Double_angle_brackets">Double angle brackets</a>
<li><a href="#Cpp0x_Explicit_conversion_operators">Explicit conversion operators</a>
<li><a href="#Cpp0x_Alias_templates">Alias templates</a>
<li><a href="#Cpp0x_Unrestricted_unions">Unrestricted unions</a>
<li><a href="#Cpp0x_Variadic_templates">Variadic templates</a>
<li><a href="#Cpp0x_New_string_literals">New string literals</a>
<li><a href="#Cpp0x_User_defined_literals">User-defined literals</a>
<li><a href="#Cpp0x_Thread_local_storage">Thread-local storage</a>
<li><a href="#Cpp0x_Defaulting/deleting_of_standard_functions_on_C++_objects">Defaulting/deleting of standard functions on C++ objects</a>
<li><a href="#Cpp0x_Type_long_long_int">Type long long int</a>
<li><a href="#Cpp0x_Static_assertions">Static assertions</a>
<li><a href="#Cpp0x_Allow_sizeof_to_work_on_members_of_classes_without_an_explicit_object">Allow sizeof to work on members of classes without an explicit object</a>
</ul>
<li><a href="#Cpp0x_Standard_library_changes">Standard library changes</a>
<ul>
<li><a href="#Cpp0x_Threading_facilities">Threading facilities</a>
<li><a href="#Cpp0x_Tuple_types">Tuple types and hash tables</a>
<li><a href="#Cpp0x_Regular_expressions">Regular expressions</a>
<li><a href="#Cpp0x_General_purpose_smart_pointers">General-purpose smart pointers</a>
<li><a href="#Cpp0x_Extensible_random_number_facility">Extensible random number facility</a>
<li><a href="#Cpp0x_Wrapper_reference">Wrapper reference</a>
<li><a href="#Cpp0x_Polymorphous_wrappers_for_function_objects">Polymorphous wrappers for function objects</a>
<li><a href="#Cpp0x_Type_traits_for_metaprogramming">Type traits for metaprogramming</a>
<li><a href="#Cpp0x_Uniform_method_for_computing_return_type_of_function_objects">Uniform method for computing return type of function objects</a>
</ul>
</ul>
</div>
<!-- INDEX -->



<H2><a name="Cpp0x_Introduction"></a>7.1 Introduction</H2>


<p>This chapter gives you a brief overview about the SWIG
implementation of the C++0x standard. This part of SWIG is still a work in
progress. Initial C++0x support for SWIG was written during the
Google Summer of Code 2009 period.</p>
<p>SWIG supports all the new C++ syntax changes with some minor limitations
(decltype expressions, variadic templates number). Wrappers for the
new STL types (unordered_ containers, result_of, tuples) are not supported
yet.</p>

<H2><a name="Cpp0x_Core_language_changes"></a>7.2 Core language changes</H2>


<H3><a name="Cpp0x_Rvalue_reference_and_move_semantics"></a>7.2.1 Rvalue reference and move semantics</H3>


<p>SWIG correctly parses the new operator &amp;&amp; the same as the reference operator &amp;.</p>

<p>The wrapper for the following code is correctly produced:</p>
<div class="code"><pre>
class MyClass {
  MyClass(MyClass&amp;&amp; p) : ptr(p.ptr) {p.ptr = 0;}
  MyClass&amp; operator=(MyClass&amp;&amp; p) {
    std::swap(ptr, p.ptr);
    return *this;
  }
};
</pre></div>

<H3><a name="Cpp0x_Generalized_constant_expressions"></a>7.2.2 Generalized constant expressions</H3>


<p>SWIG correctly parses the keyword <tt>constexpr</tt>, but ignores its functionality. Constant functions cannot be used as constants.</p>

<div class="code"><pre>
constexpr int myConstFunc() { return 10; }
const int a = myConstFunc(); // results in error
</pre></div>

<p>Users needs to use values or predefined constants when defining the new constant value:</p>

<div class="code"><pre>
#define MY_CONST 10
constexpr int myConstFunc() { return MY_CONST; }
const int a = MY_CONST; // ok
</pre></div>

<H3><a name="Cpp0x_Extern_template"></a>7.2.3 Extern template</H3>


<p>SWIG correctly parses the keywords <tt>extern template</tt>. However, the explicit template instantiation is not used by SWIG, a <tt>%template</tt> is still required.</p>


<div class="code"><pre>
extern template class std::vector&lt;MyClass&gt;; // explicit instantiation

...

class MyClass {
public:
  int a;
  int b;
};
</pre></div>

<H3><a name="Cpp0x_Initializer_lists"></a>7.2.4 Initializer lists</H3>


<p>Constructors using the std::initializer_list class are removed
from the wrapped class, because the only way to access such a
constructor is at compile time using the "= {}" assignment.</p>
<p>Users should add another constructor with specific arguments
filling the class members manually.</p>

<p>For now, if a user wants to fill the class components like this:</p>

<div class="code"><pre>
class A {
public:
  A( std::initializer_list&lt;int&gt; );
};
A a1 = {1,2,3,4};
</pre></div>

<p>You should add another constructor using the std::vector for example:</p>

<div class="code"><pre>
class A {
public:
  A( std::initializer_list&lt;int&gt; );
  A( std::vector&lt;int&gt; );
};
A a1 = {1,2,3,4};
</pre></div>

<p>And call it from your target language, for example, in Python:</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; a2 = A( [1,2,3,4] )
</pre></div>

<H3><a name="Cpp0x_Uniform_initialization"></a>7.2.5 Uniform initialization</H3>


<p>The curly brackets {} for member initialization are fully
supported by SWIG:</p>

<div class="code"><pre>
struct BasicStruct {
 int x;
 double y;
};
 
struct AltStruct {
  AltStruct(int x, double y) : x_{x}, y_{y} {}
 
  int x_;
  double y_;
};

BasicStruct var1{5, 3.2}; // only fills the struct components
AltStruct var2{2, 4.3};   // calls the constructor
</pre></div>

<p>Uniform initialization does not affect usage from the target language, for example in Python:</p>

<div class="targetlang"><pre>
&gt;&gt;&gt; a = AltStruct(10, 142.15)
&gt;&gt;&gt; a.x_
10
&gt;&gt;&gt; a.y_
142.15
</pre></div>

<H3><a name="Cpp0x_Type_inference"></a>7.2.6 Type inference</H3>


<p>SWIG supports <tt>decltype()</tt> with some limitations. Single
variables are allowed, however, expressions are not supported yet. For
example, the following code will work:</p>
<div class="code"><pre>
int i;
decltype(i) j;
</pre></div>

<p>However, using an expression inside the decltype results in syntax error:</p>
<div class="code"><pre>
int i; int j;
decltype(i+j) k;  // syntax error
</pre></div>

<H3><a name="Cpp0x_Range_based_for_loop"></a>7.2.7 Range-based for-loop</H3>


<p>This feature is part of the implementation block only. SWIG
ignores it.</p>

<H3><a name="Cpp0x_Lambda_functions_and_expressions"></a>7.2.8 Lambda functions and expressions</H3>


<p>SWIG correctly parses the Lambda functions syntax. For example:</p>
<div class="code"><pre>
auto sum = [](int x, int y) -&gt; int { return x+y; };
</pre></div>

<p>The lambda functions are removed from the wrapper class for now, because of the lack of support
for closures (scope of the lambda functions) in the target languages.</p>

<H3><a name="Cpp0x_Alternate_function_syntax"></a>7.2.9 Alternate function syntax</H3>


<p>SWIG fully supports the new definition of functions. For example:</p>
<div class="code"><pre>
struct SomeStruct {
  int FuncName(int x, int y);
};
</pre></div>

<p>can now be written as in C++0x:</p>

<div class="code"><pre>
struct SomeStruct {
  auto FuncName(int x, int y) -&gt; int;
};
 
auto SomeStruct::FuncName(int x, int y) -&gt; int {
  return x + y;
}
</pre></div>

<p>The usage in the target languages remains the same, for example in Python:</p>

<div class="targetlang"><pre>
&gt;&gt;&gt; a = SomeStruct()
&gt;&gt;&gt; a.FuncName(10,5)
15
</pre></div>

<p>SWIG will also deal with type inference for the return type, as per the limitations described earlier. For example:</p>
<div class="code"><pre>
auto square(float a, float b) -&gt; decltype(a);
</pre></div>

<H3><a name="Cpp0x_Object_construction_improvement"></a>7.2.10 Object construction improvement</H3>


<p>SWIG correctly parses and includes the external functions
(constructor delegation and constructor inheritance) into the class
using the <tt>using</tt> keyword.</p>

<div class="code"><pre>
class BaseClass {
public:
  BaseClass(int iValue);
};

class DerivedClass: public BaseClass {
  public:
  using BaseClass::BaseClass; // Adds DerivedClass(int) constructor
};
</pre></div>

<H3><a name="Cpp0x_Null_pointer_constant"></a>7.2.11 Null pointer constant</H3>


<p>The <tt>nullptr</tt> constant is largely unimportant in wrappers. In the few places it has an effect, it is treated like <tt>NULL</tt>.</p>

<H3><a name="Cpp0x_Strongly_typed_enumerations"></a>7.2.12 Strongly typed enumerations</H3>


<p>SWIG parses the new <tt>enum class</tt> syntax and forward declarator for the enums:</p>
<div class="code"><pre>
enum class MyEnum : unsigned int;
</pre></div>

<p>The strongly typed enumerations are treated the same as the ordinary and anonymous enums.
This is because SWIG doesn't support nested classes. This is usually not a problem, however, 
there may be some name clashes. For example, the following code:</p>

<div class="code"><pre>
class Color {
  enum class PrintingColors : unsigned int {
    Cyan, Magenta, Yellow, Black
  };
  
  enum class BasicColors {
    Red, Green, Blue
  };
  
  enum class AllColors {
    // produces warnings because of duplicate names
    Yellow, Orange, Red, Magenta, Blue, Cyan, Green, Pink, Black, White
  };
};
</pre></div>

<p>A workaround is to write these as a series of separated classes containing anonymous enums:</p>

<div class="code"><pre>
class PrintingColors {
  enum : unsigned int {
    Cyan, Magenta, Yellow, Black
  };
};

class BasicColors {
  enum : unsigned int {
    Red, Green, Blue
  };
};

class AllColors {
  enum : unsigned int {
    Yellow, Orange, Red, Magenta, Blue, Cyan, Green, Pink, Black, White
  };
};
</pre></div>

<H3><a name="Cpp0x_Double_angle_brackets"></a>7.2.13 Double angle brackets</H3>


<p>SWIG correctly parses the symbols &gt;&gt; as closing the
template block, if found inside it at the top level, or as the right
shift operator &gt;&gt; otherwise.</p>

<div class="code"><pre>
std::vector&lt;std::vector&lt;int&gt;&gt; myIntTable;
</pre></div>

<H3><a name="Cpp0x_Explicit_conversion_operators"></a>7.2.14 Explicit conversion operators</H3>


<p>SWIG correctly parses the keyword <tt>explicit</tt> both for operators and constructors.
For example:</p>

<div class="code"><pre>
class U {
public:
        int u;
};

class V {
public:
        int v;
};

class TestClass {
public:
        //implicit converting constructor
        TestClass( U const &amp;val ) { t=val.u; }
        // explicit constructor
        explicit TestClass( V const &amp;val ) { t=val.v; }

        int t;
};
</pre></div>

<p>
The usage of explicit constructors and operators is somehow specific to C++ when assigning the value
of one object to another one of different type or translating one type to another. It requires both operator and function overloading features,
which are not supported by the majority of SWIG target languages. Also the constructors and operators are not particulary useful in any
SWIG target languages, because all use their own facilities (eg. classes Cloneable and Comparable in Java)
to achieve particular copy and compare behaviours.
</p>

<H3><a name="Cpp0x_Alias_templates"></a>7.2.15 Alias templates</H3>

<p>
The following is an example of an alias template:

<div class="code"><pre>
template&lt; typename T1, typename T2, int &gt;
class SomeType {
  T1 a;
  T2 b;
  int c;
};

template&lt; typename T2 &gt;
using TypedefName = SomeType&lt;char*, T2, 5&gt;;
</pre></div>

<p>
These are partially supported as SWIG will parse these and identify them, however, they are ignored as they are not added to the type system. A warning such as the following is issued:
</p>

<div class="shell">
<pre>
example.i:13: Warning 342: The 'using' keyword in template aliasing is not fully supported yet.
</pre>
</div>

<p>
Similarly for non-template type aliasing:
</p>

<div class="code"><pre>
using PFD = void (*)(double); // New introduced syntax
</pre></div>

<p>
A warning will be issued:
</p>

<div class="shell">
<pre>
example.i:17: Warning 341: The 'using' keyword in type aliasing is not fully supported yet.
</pre>
</div>


<p>The equivalent old style typedefs can be used as a workaround:</p>

<div class="code"><pre>
typedef void (*PFD)(double);  // The old style
</pre></div>

<H3><a name="Cpp0x_Unrestricted_unions"></a>7.2.16 Unrestricted unions</H3>


<p>SWIG fully supports any type inside a union even if it does not
define the trivial constructor. For example, the wrapper for the following
code is correctly produced:</p>

<div class="code"><pre>
struct point {
  point() {}
  point(int x, int y): x_(x), y_(y) {}
  int x_, y_;
};

union P {
  int z;
  double w;
  point p;  // Illegal in C++; point has a non-trivial constructor.  However, this is legal in C++0x.
} p1;
</pre></div>

<H3><a name="Cpp0x_Variadic_templates"></a>7.2.17 Variadic templates</H3>


<p>SWIG fully supports the variadic templates syntax (inside the &lt;&gt;
block, variadic class inheritance and variadic constructor and
initializers) with some limitations. The following code is correctly parsed:</p>

<div class="code"><pre>
template &lt;typename... BaseClasses&gt; class ClassName : public BaseClasses... {
public:
   ClassName (BaseClasses&amp;&amp;... baseClasses) : BaseClasses(baseClasses)... {}
}
</pre></div>

<p>Support for the variadic sizeof() function was also introduced:</p>

<div class="code"><pre>
const int SIZE = sizeof...(ClassName&lt;int, int&gt;);
</pre></div>

<p>For now however, the <tt>%template</tt> directive only accepts at most the number of
arguments defined in the original template&lt;&gt; block:</p>

<div class="code"><pre>
%template(MyVariant1) ClassName&lt;&gt;         // ok
%template(MyVariant2) ClassName&lt;int&gt;      // ok
%template(MyVariant3) ClassName&lt;int, int&gt; // too many arguments
</pre></div>

<H3><a name="Cpp0x_New_string_literals"></a>7.2.18 New string literals</H3>


<p>SWIG fully supports unicode string constants and raw string literals.</p>

<div class="code"><pre>
// New string literals
wstring         aa =  L"Wide string";
const char     *bb = u8"UTF-8 string";
const char16_t *cc =  u"UTF-16 string";
const char32_t *dd =  U"UTF-32 string";

// Raw string literals
const char      *xx =        ")I'm an \"ascii\" \\ string.";
const char      *ee =   R"XXX()I'm an "ascii" \ string.)XXX"; // same as xx
wstring          ff =  LR"XXX(I'm a "raw wide" \ string.)XXX";
const char      *gg = u8R"XXX(I'm a "raw UTF-8" \ string.)XXX";
const char16_t  *hh =  uR"XXX(I'm a "raw UTF-16" \ string.)XXX";
const char32_t  *ii =  UR"XXX(I'm a "raw UTF-32" \ string.)XXX";
</pre></div>

<p>Note: SWIG currently incorrectly parses the odd number of double quotes
inside the string due to SWIG's C++ preprocessor.</p>

<H3><a name="Cpp0x_User_defined_literals"></a>7.2.19 User-defined literals</H3>


<p>SWIG correctly parses the new <tt>operator""_mysuffix()</tt> functions.</p>

<div class="code"><pre>
OutputType operator "" _mySuffix(const char * string_values, size_t num_chars);
OutputType operator "" _mySuffix(const wchar_t * string_values, size_t num_chars);
OutputType operator "" _mySuffix(const char16_t * string_values, size_t num_chars);
OutputType operator "" _mySuffix(const char32_t * string_values, size_t num_chars);
OutputType operator "" _mySuffix(int value);
</pre></div>

<p>The %rename currently doesn't parse the double quotes. Please
rename the functions in the code using the #define preprocessor directive.</p>

<H3><a name="Cpp0x_Thread_local_storage"></a>7.2.20 Thread-local storage</H3>


<p>SWIG correctly parses the <tt>thread_local</tt> keyword. For example, a variable
reachable by the current thread can be defined as:</p> 

<div class="code"><pre>
struct A {
   thread_local int val;
};
</pre></div>

<p>The new C++0x threading libraries are ignored because each SWIG target language offers
its own threading facilities.</p>

<H3><a name="Cpp0x_Defaulting/deleting_of_standard_functions_on_C++_objects"></a>7.2.21 Defaulting/deleting of standard functions on C++ objects</H3>


<p>SWIG correctly parses the <tt>= delete</tt> and <tt>= default</tt>
keywords. For example:</p>

<div class="code"><pre>
struct NonCopyable {
  NonCopyable&amp; operator=(const NonCopyable&amp;) = delete; /* Removes operator= */
  NonCopyable(const NonCopyable&amp;) = delete;            /* Removed copy constructor */
  NonCopyable() = default;                             /* Explicitly allows the empty constructor */
  void *operator new(std::size_t) = delete;            /* Removes new NonCopyable */
};
</pre></div>

<p>This feature is specific to C++ only. The defaulting/deleting is currently ignored, because SWIG
automatically produces wrappers for special constructors and operators specific to the target language.</p>

<H3><a name="Cpp0x_Type_long_long_int"></a>7.2.22 Type long long int</H3>


<p>SWIG correctly parses and uses the new <tt>long long</tt> type already introduced in C99 some time ago.</p>

<H3><a name="Cpp0x_Static_assertions"></a>7.2.23 Static assertions</H3>


<p>SWIG correctly parses and calls the new <tt>static_assert</tt> function.</p>

<div class="code"><pre>
template &lt;typename T&gt;
struct Check {
  static_assert(sizeof(int) &lt;= sizeof(T), "not big enough");
};
</pre></div>

<H3><a name="Cpp0x_Allow_sizeof_to_work_on_members_of_classes_without_an_explicit_object"></a>7.2.24 Allow sizeof to work on members of classes without an explicit object</H3>


<p>SWIG correctly calls the sizeof() on types as well as on the
objects. For example:</p>

<div class="code"><pre>
struct A {
  int member;
};

const int SIZE = sizeof(A::member); // does not work with C++03. Okay with C++0x
</pre></div>

<p>In Python:</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; SIZE
8
</pre></div>

<H2><a name="Cpp0x_Standard_library_changes"></a>7.3 Standard library changes</H2>


<H3><a name="Cpp0x_Threading_facilities"></a>7.3.1 Threading facilities</H3>


<p>SWIG does not currently wrap or use any of the new threading
classes introduced (thread, mutex, locks, condition variables, task). The main reason is that
SWIG target languages offer their own threading facilities that do not rely on C++.</p>

<H3><a name="Cpp0x_Tuple_types"></a>7.3.2 Tuple types and hash tables</H3>


<p>SWIG does not wrap the new tuple types and the unordered_ container classes yet. Variadic template support is working so it is possible to
include the tuple header file; it is parsed without any problems.</p>

<H3><a name="Cpp0x_Regular_expressions"></a>7.3.3 Regular expressions</H3>


<p>SWIG does not wrap the new C++0x regular expressions classes, because the SWIG target languages use their own facilities for this.</p>

<H3><a name="Cpp0x_General_purpose_smart_pointers"></a>7.3.4 General-purpose smart pointers</H3>


<p>
SWIG provides special smart pointer handling for <tt>std::tr1::shared_ptr</tt> in the same way it has support for <tt>boost::shared_ptr</tt>.
There is no special smart pointer handling available for <tt>std::weak_ptr</tt> and <tt>std::unique_ptr</tt>.
</p>

<H3><a name="Cpp0x_Extensible_random_number_facility"></a>7.3.5 Extensible random number facility</H3>


<p>This feature extends and standardizes the standard library only and does not effect the C++ language and SWIG.</p>

<H3><a name="Cpp0x_Wrapper_reference"></a>7.3.6 Wrapper reference</H3>


<p>The new ref and cref classes are used to instantiate a parameter as a reference of a template function. For example:</p>

<div class="code"><pre>
void f( int &amp;r )  { r++; }
 
// Template function.
template&lt; class F, class P &gt; void g( F f, P t )  { f(t); }
 
int main() {
  int i = 0 ;
  g( f, i ) ;  // 'g&lt;void ( int &amp;r ), int&gt;' is instantiated
               // then 'i' will not be modified.
  cout &lt;&lt; i &lt;&lt; endl ;  // Output -&gt; 0
 
  g( f, ref(i) ) ;  // 'g&lt;void(int &amp;r),reference_wrapper&lt;int&gt;&gt;' is instantiated
                    // then 'i' will be modified.
  cout &lt;&lt; i &lt;&lt; endl ;  // Output -&gt; 1
}
</pre></div>

<p>The ref and cref classes are not wrapped by SWIG because the SWIG target languages do not support referencing.</p>

<H3><a name="Cpp0x_Polymorphous_wrappers_for_function_objects"></a>7.3.7 Polymorphous wrappers for function objects</H3>


<p>SWIG fully supports function template wrappers and function objects:</p>

<div class="code"><pre>
function&lt;int ( int, int )&gt; pF;   // function template wrapper

struct Test {
  bool operator()( short x, short y ); // function object
};
</pre></div>

<H3><a name="Cpp0x_Type_traits_for_metaprogramming"></a>7.3.8 Type traits for metaprogramming</H3>


<p>The new C++ metaprogramming is useful at compile time and is aimed specifically for C++ development:</p>

<div class="code"><pre>
// First way of operating.
template&lt; bool B &gt; struct algorithm {
  template&lt; class T1, class T2 &gt; int do_it( T1&amp;, T2&amp; )  { /*...*/ }
};
// Second way of operating.
template&lt;&gt; struct algorithm&lt;true&gt; {
  template&lt; class T1, class T2 &gt; int do_it( T1, T2 )  { /*...*/ }
};
// Instantiating 'elaborate' will automatically instantiate the correct way to operate.
template&lt; class T1, class T2 &gt; int elaborate( T1 A, T2 B ) {
  // Use the second way only if 'T1' is an integer and if 'T2' is
  // in floating point, otherwise use the first way.
  return algorithm&lt; is_integral&lt;T1&gt;::value &amp;&amp; is_floating_point&lt;T2&gt;::value &gt;::do_it( A, B );
}
</pre></div>

<p>SWIG correctly parses the template specialization, template types and values inside the &lt;&gt; block and the new helper functions: is_convertible, is_integral, is_const etc.
However, SWIG still explicitly requires concrete types when using the <tt>%template</tt> directive, so the C++ metaprogramming features are not really of interest at runtime in the target languages.</p>

<H3><a name="Cpp0x_Uniform_method_for_computing_return_type_of_function_objects"></a>7.3.9 Uniform method for computing return type of function objects</H3>


<p>SWIG does not wrap the new result_of class introduced in the &lt;functional&gt; header and map the result_of::type to the concrete type yet. For example:</p>
<div class="code"><pre>
%inline %{
#include &lt;functional&gt;
double square(double x) {
        return (x * x);
}

template&lt;class Fun, class Arg&gt;
typename std::result_of&lt;Fun(Arg)&gt;::type test_result_impl(Fun fun, Arg arg) {
        return fun(arg);
}
%}

%template(test_result) test_result_impl&lt;double(*)(double), double&gt;;
%constant double (*SQUARE)(double) = square;
</pre></div>

<p>will result in:</p>

<div class="targetlang"><pre>
&gt;&gt;&gt; test_result_impl(SQUARE, 5.0)
&lt;SWIG Object of type 'std::result_of&lt; Fun(Arg) &gt;::type *' at 0x7faf99ed8a50&gt;
</pre></div>

<p>Instead, please use <tt>decltype()</tt> where possible for now.</p>
</body>
</html>