Uploaded image for project: 'ROOT'
  1. ROOT
  2. ROOT-11012

Definition of operator() in derived class hides operator[] of base class

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • High
    • Resolution: Clarified
    • master, 6.22/02
    • None
    • PyROOT
    • None
    • Python3,Python2
      Linux

    Description

      From the following report in the forum:

      https://root-forum.cern.ch/t/root-6-22-00-02-operators-from-c-base-class-are-not-propagated-in-pyroot

      The definition of operator() in a derived class "hides" the definition of operator[] in the base class when invoking operator[] in an object of the derived class in PyROOT.

      Reproducer:

      import cppyy
       
      code = '''
      struct Base {
          const std::string& operator[](const std::string& x) {
              return x;
          }
      };
       
      struct Problematic: public Base {
          // This definition hides operator[] in PyROOT
          double operator()(double x) {
              return x+1;
          }
      };
      '''
       
      cppyy.gbl.gInterpreter.Declare(code)
       
      problem = cppyy.gbl.Problematic()
       
      try:
          # ROOT 6.22/00 -- modern PyROOT -- will fail
          # ROOT 6.20/04 -- legacy PyROOT -- will succeed
          print("Trying to call Problematic::operator[](const std::string&):")
          _ = problem['Call succeeded']
          print(_, '\n')
      except TypeError as e:
          print("Call to Problematic::operator[](const std::string&) failed\n")
          print("Exception", e)
      

      prints

      Trying to call Problematic::operator[](const std::string&):
      Call to Problematic::operator[](const std::string&) failed
       
      Exception double Problematic::operator()(double x) =>
          TypeError: could not convert argument 1 (must be real number, not str)
      

      So it seems operator()(double) is being tried when calling operator[](string) on a Problematic (derived class) object.

      Attachments

        Activity

          People

            etejedor Enric Tejedor Saavedra
            etejedor Enric Tejedor Saavedra
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: