Use Function and Member Function Templates
Overloaded Functions
MATLAB® supports C++ function and member function templates. The C++ interface
generates a MATLAB function overload for each function template instantiation using a valid
MATLAB name based on the C++ function name. Suppose that you have this C++ header
file that defines a function template show
and provides instantiations
for types int
, double
, and const
A
.
class A{}; // User type
template<typename T> void show(T a) {}
template void show<int>(int);
template void show<double>(double);
template<> void show<const A &>(const A& a){}
Build interface libname
, then display help for the
show
function. MATLAB displays the calling syntax for three functions.
help clib.libname.show
clib.libname.show Representation of C++ function show. clib.libname.show(a) Input Arguments a int32 clib.libname.show Representation of C++ function show. clib.libname.show(a) Input Arguments a double clib.libname.show Representation of C++ function show. clib.libname.show(a) Input Arguments a read-only clib.libname.A
If you type:
var = pi; clib.libname.show(var)
then MATLAB chooses the signature with input type double
.
Unique Function Names
The C++ interface also generates unique function names based on the signature types. To
view the unique names for the show
function, type:
help clib.libname
Classes contained in clib.libname: A - clib.libname.A Representation of C++ class A. Functions contained in clib.libname: show - clib.libname.show Representation of C++ function show. show_AConst__ - clib.libname.show Representation of C++ function show. show - clib.libname.show Representation of C++ function show. show_double_ - clib.libname.show Representation of C++ function show. show - clib.libname.show Representation of C++ function show. show_int_ - clib.libname.show Representation of C++ function show.
To call the type-specific function for input of type double
,
type:
clib.libname.show_double_(var)
Publishers can modify these names when building an interface to the library. For more information, see Customize Function Template Names.