Menu

#678 -copyctor bug?!

None
closed
5
2023-07-29
2006-05-10
nitro
No

Hello,

Say I have this config setup:

class Base1
{
public:
Base1();
}

class Base2
{
public:
Base2();
protected:
Base2(Base1& other); // copyctor
}

class Derived : public Base1, public Base2
{
public:
Derived();
};

If I try to swig this code with -copyctor enabled, then
SWIG will generate a call to the copy constructor of
Derived. However, Derived does not have a copy
constructor, so this leads to a compilation error.

-Matthias

Discussion

  • Olly Betts

    Olly Betts - 2011-08-01
    • status: open --> closed-fixed
     
  • Olly Betts

    Olly Betts - 2011-08-01

    I checked this with trunk r12766 and it seems it's since been fixed. I had to add ";" after "}" for the first two classes, then wrap the snippet in %inline { [...] }, saved as 1485859.i, then processed with:

    ./preinst-swig -copyctor -python -c++ -module tmp 1485859.i
    g++ -fPIC -I /usr/include/python2.6 -Wall -W -shared -o tmp.so 1485859_wrap.cxx

     
  • William Fulton

    William Fulton - 2011-08-05

    The generated code from the testcase as shown does indeed compile under g++-4.4.3 for swig-1.3.31 and 2.0.4. Looks like there is a mistake in the original code. If

    Base2(Base1& other); // copyctor

    is replaced with

    Base2(Base2& other); // non-const copyctor

    then a copy constructor is generated resulting in a compiler error in the wrappers:

    example_wrap.cxx:327: error: no matching function for call to ‘Derived::Derived(const Derived&)’
    example_wrap.cxx:231: note: candidates are: Derived::Derived()
    example_wrap.cxx:229: note: Derived::Derived(Derived&)

     
  • William Fulton

    William Fulton - 2011-08-05
    • status: closed-fixed --> open
     
  • Olly Betts

    Olly Betts - 2022-01-25

    Still fails with current git master (7ab24e7e865e0f1281f002fefdaba11b80416ea2), though the error now seems to be slightly different:

    test_wrap.cxx: In function PyObject* _wrap_new_Derived__SWIG_1(PyObject*, Py_ssize_t, PyObject**):
    test_wrap.cxx:3283:35: error: binding reference of type Derived& to const Derived discards qualifiers
     3283 |   result = (Derived *)new Derived((Derived const &)*arg1);
          |                                   ^~~~~~~~~~~~~~~~~~~~~~
    test_wrap.cxx:3100:7: note:   initializing argument 1 of Derived::Derived(Derived&)
     3100 | class Derived : public Base1, public Base2
          |       ^~~~~~~
    
     
  • Olly Betts

    Olly Betts - 2022-12-07

    Reproduced with 8a24c19d2621e0de6a4429a039dc75d3a761acd4.

     
  • Olly Betts

    Olly Betts - 2023-07-25

    Reproduced with 595009cf2ff952d963a663113e82b032c4fa770f.

     
  • Olly Betts

    Olly Betts - 2023-07-29

    Reproduced with 5e1a37c6c562b7cb62c62e439c5b6b647e6d6df5 (retested because that commit fixes a copyctor bug).

     
  • William Fulton

    William Fulton - 2023-07-29

    This bug is a problem with non-const copy constructors in an inheritance chain. SWIG always assumed the implicit copy constructor was defined with a const reference parameter. For reference, https://en.cppreference.com/w/cpp/language/copy_constructor outlines nicely when the implicit constructor uses a const or non-const reference parameter.

    Fixed in 74e1deef6bd86135ee2a5f007b6347202813d143.

     
  • William Fulton

    William Fulton - 2023-07-29
    • status: open --> closed
    • assigned_to: David M. Beazley --> William Fulton
    • Group: -->
     

Log in to post a comment.