Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to handle std::forward_list<T> ? #2874

Open
pp4x opened this issue Apr 16, 2024 · 2 comments
Open

Question: how to handle std::forward_list<T> ? #2874

pp4x opened this issue Apr 16, 2024 · 2 comments

Comments

@pp4x
Copy link

pp4x commented Apr 16, 2024

Apologies in advance if this is not the right place to ask questions. For reasons of backward compatibility (especially ABI compatibility), I am stuck with std::forward_list. So, switching to std::list is not possible.

#pragma once

#include <fmt/core.h>
#include <forward_list>

#ifdef SWIG
%ignore A::A( A && );
%ignore A::operator=( A && );
%ignore A::foo( A && );
#endif

class A {
public:
   A() = default;
   A(const A &) = default;
   A(A &&) = default;
   A& operator=(const A &) = default;
   A& operator=(A &&) = default;
   ~A() = default;

   void foo( const std::forward_list<A> & ) {
      fmt::print("foo(const &)\n");
   }
   void foo( std::forward_list<A> && ) {
      fmt::print("foo(&&)\n");
   }

   A clone() { return *this; }
};

And this interface file

%module foo

%{
#include "foo.h"
%}

%include "foo.h"

It compiles (this is a conversion to python) but, when trying this python script is run:

from foo import A

a = A()
a.foo([])
l = [a, a.clone()]
a.foo(l)

this is the error I get:

TypeError: in method 'A_foo', argument 2 of type 'std::forward_list< A,std::allocator< A > > const &'
Additional information:
Wrong number or type of arguments for overloaded function 'A_foo'.
  Possible C/C++ prototypes are:
    A::foo(std::forward_list< A,std::allocator< A > > const &)
    A::foo(std::forward_list< A,std::allocator< A > > &&)

Traceback (most recent call last):
  File "/tmp/./testfoo.py", line 6, in <module>
    a.foo([])
TypeError: in method 'A_foo', argument 2 of type 'std::forward_list< A,std::allocator< A > > const &'
Additional information:
Wrong number or type of arguments for overloaded function 'A_foo'.
  Possible C/C++ prototypes are:
    A::foo(std::forward_list< A,std::allocator< A > > const &)
    A::foo(std::forward_list< A,std::allocator< A > > &&)

If this was std::list or any other mapped container, then the following interface:

%module foo

%{
#include "foo.h"
%}

%include <std_list.i>
%template() std::list<A>;

%include "foo.h"

Would make things work.

Since this is not the case, I assume the best way (if not the only way) to handle this ambiguity would be to map the type on its own interface file. So I am looking for documentation on how to map a new container type or any workaround to resolve that ambiguity.

Any ideas?

@pp4x pp4x changed the title Question: how to deal with std::forward_list<T> ? Question: how to handle std::forward_list<T> ? Apr 16, 2024
@ojwb
Copy link
Member

ojwb commented Apr 16, 2024

Implementing std_forward_list.i in SWIG's library seems the best approach. It could be heavily based on std_list.i.

@pp4x
Copy link
Author

pp4x commented Apr 17, 2024

I made my attempt at this, starting from std_list.i, but got stuck with a C++ compile error, because std::forward_list doesn't provide a method insert, required a function assign implementation in std_container.i. Instead, the there is a function called insert_after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants