Menu

#960 Namespace breaks %ignore with unbound C++ operator<<

None
closed
python (260)
5
2024-01-15
2008-11-03
No

NB. This may be in the C++ parser rather than anything Python-specific, but I've only tested in Python.

I've been trying to hide a C++ operator<< function (2 args unbound, i.e. not a class method) using the SWIG %ignore directive. It seems that if this stream function is declared as a friend in the class to be streamed, inside a namespace, then the %ignore matching fails, even in the case of an exact textual match. Here's an example hepmc.i file, with the C++ code to be mapped represented inline:

-----
%module hepmc

// These don't work:
namespace HepMC {
%ignore operator<<;
%ignore operator<<(std::ostream&, const GenParticle&);
}
%ignore HepMC::operator<<;
%ignore HepMC::operator<<(std::ostream&, const GenParticle&);

// This works:
//%ignore operator<<;
//%ignore operator<<(std::ostream&, const GenParticle&);

/////////////////////////////////////////
// Equiv to: %include "HepMC/GenParticle.h"
namespace HepMC {
class GenParticle {
// [...]
friend std::ostream& operator<<( std::ostream&, const GenParticle& );
};

std::ostream& operator<<( std::ostream& os, const GenParticle& gp) {
os << gp.foo();
return os;
}
}
/////////////////////////////////////////
-----

The above results in an error " Warning(503): Can't wrap 'operator <<' unless renamed to a valid identifier." referring specifically to the friend statement, unless the "coarse match" %ignores under the "This works" comment are included. The equivalent %ignores which are specific, one way or another, about the namespace don't match the operator<< referred to in the friend statement.

Note that this bug seems to be specific to the friend statement --- if you remove the friend declaration in GenParticle, the %ignore works properly.

Discussion

  • Olly Betts

    Olly Betts - 2022-03-19

    Reproducible with current git master.

     
  • William Fulton

    William Fulton - 2024-01-15
    • status: open --> closed
    • assigned_to: William Fulton
    • Group: -->
     
  • William Fulton

    William Fulton - 2024-01-15

    Fixed by a6ab9145115aa124ff9c98e2b3c39fde9f205760.

    All 6 of the %ignore directives shown now work.

     

Log in to post a comment.