You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<H4><aname="CPlusPlus11_move_only">7.2.1.1 Movable and move-only types</a></H4>
130
+
131
+
132
+
<p>
133
+
SWIG has traditionally relied on wrapped C++ types to be copy constructible or copy assignable, either via an explicit or implicit copy constructor and copy assignment operator.
134
+
Prior to C++11, a function could not return nor take a type by value that was not copyable.
135
+
In C++11 this is no longer the case. A type can also be movable if it has has a move constructor and a move assignment operator.
136
+
A move-only type is movable but not copyable; it has both the copy constructor and copy assignment operator deleted.
137
+
Movable types can appear in function signatures for passing 'by value' and in C++11 the object can then be moved rather than copied.
138
+
</p>
139
+
140
+
<p>
141
+
SWIG has been enhanced with support for both copyable and/or movable types but this is currently just for function return values.
142
+
</p>
143
+
144
+
<p>
145
+
The support for function return values is generically implemented in the "out" <tt>SWIGTYPE</tt> typemap which supports any type, including copyable, movable and move-only types.
146
+
The typemap code is very simple and written so that the compiler will call the move constructor if possible,
147
+
otherwise the copy constructor:
148
+
</p>
149
+
150
+
<divclass="code"><pre>
151
+
%typemap(out) SWIGTYPE %{
152
+
$result = new $1_ltype($1);
153
+
%}
154
+
</pre></div>
155
+
156
+
<p>
157
+
The above typemap is for C# and when used to wrap a move-only type such as:
158
+
</p>
159
+
160
+
<divclass="code"><pre>
161
+
struct MoveOnly {
162
+
int val;
163
+
MoveOnly(): val(0) {}
164
+
165
+
MoveOnly(const MoveOnly &) = delete;
166
+
MoveOnly(MoveOnly &&) = default;
167
+
168
+
MoveOnly & operator=(const MoveOnly &) = delete;
169
+
MoveOnly & operator=(MoveOnly &&) = default;
170
+
171
+
static MoveOnly create() { return MoveOnly(); }
172
+
};
173
+
</pre></div>
174
+
175
+
<p>
176
+
will generate wrapper code for the <tt>create</tt> factory method:
<tt>SwigValueWrapper</tt> is covered in <ahref="SWIGPlus.html#SWIGPlus_nn19">Pass and return by value</a>.
192
+
Note that the generated code could be optimised further using the <ahref="Typemaps.html#Typemaps_optimal">"optimal" attribute</a> in the "out" typemap.
193
+
</p>
194
+
195
+
<p>
196
+
There is currently only partial support for move-only types as
197
+
support for move-only types used as a parameter in a function, that are passed 'by value', is not yet available.
198
+
</p>
199
+
200
+
<p>
201
+
<b>Compatibility note:</b>
202
+
SWIG-4.1.0 introduced support for taking advantage of types with move semantics and wrapping functions that return movable or move-only types 'by value'.
0 commit comments