std:optional question
Julien Brulé via Swig-user <[email protected]> Thu, 19 Feb 2026 16:15:52 +0100
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <[email protected]> |
Hello the list,
sorry it's a noob question ...
how to deal with a c++ function with a signature including a
std::optional parameter for python?
i saw that
https://stackoverflow.com/questions/66530923/swig-python-wrapper-for-stdoptional
and
https://gist.github.com/vadz/44bcb03ea2ca8a370012a1f47aa00eaf
but not sure how to proceed.
i put
%typemap(in) std::optional<std::string> %{
if ($input == Py_None) {
$1 = std::optional<std::string>();
} else {
$1 = std::optional<std::string>(PyString_AsString($input));
}
%}
in my .i file
but i encounter the error
TypeError: Wrong number or type of arguments for overloaded function
'say_msg'.
Possible C/C++ prototypes are:
say_msg(std::string,std::optional< string >)
say_msg(std::string)
my function is for example
void say_msg(std::string name, std::optional<string> msg = std::nullopt);
do i need a typecheck as
https://www.swig.org/Doc4.1/Typemaps.html#Typemaps_nn27
but can t find a good SWIG_TYPECHECK_XXX candidate
thanks for your advices