Skip to content

PyROOT incorrect/unstable overload resolution with enum class. #7077

Open
@pcanal

Description

@pcanal

With master (commit 7f3f342) (but has been present for a long time), PyROOT overload resolution of function that takes an enum class is unstable (it appears to depend on the value rather than the type).

With the test file below compiled by ACLiC (for example) running the script:

import ROOT

ROOT.gSystem.Load("enums_cxx")

h = ROOT.Holder()
for i in range(10, 0, -1):
   h.SetTwo(i)
   ROOT.to_string(h.two)

gives

Calling to_string for enum EOne with 0
Calling to_string for enum EOne with 1
...

I.e. even-though the argument is an 'enum ETwo', the overload take an enum EOne actually called.

In the original example (requires the code for Celeritas and VecGeom to build). For a similar script:

for i in range(0, 14, 1):
   p.process_class = i
   ROOT.celeritas.to_cstring(p.process_class)

I get:

to_cstring ImportPhysicsVectorType called with 0 unknown
to_cstring ImportPhysicsVectorType called with 1 linear
to_cstring ImportPhysicsVectorType called with 2 log
to_cstring ImportPhysicsVectorType called with 3 free
to_cstring ImportTableType called with 4 ionisation_subsec
to_cstring ImportTableType called with 5 csda_range
to_cstring ImportTableType called with 6 range
to_cstring ImportTableType called with 7 secondary_range
to_cstring ImportTableType called with 8 inverse_range
to_cstring ImportTableType called with 9 lambda
to_cstring ImportTableType called with 10 sublambda
to_cstring ImportTableType called with 11 lambda_prim
to_cstring ImportProcessType called with 12 ucn
to_cstring ImportProcessClass called with 13 annihilation

i.e. the same code line given the same data members (and thus always the same type), call 3 different overloads over the loop iterations. (Funnily running the loop in reverse order leads to the expected/correct executions). I could not reproduce this instability with my simple example.

#include <stdio.h>
#include <string>

enum class EOne
{
  a,
  b,
  c,
  d
};

enum class ETwo
{
  a = 2,
  b,
  c,
  d
};

enum class EThree
{
  a = 3,
  b,
  c,
  d
};

struct Holder
{
  EOne one;
  ETwo two;
  EThree three;

  void SetTwo(int value)
  {
    two = (ETwo)value;
  }
};


std::string to_string(EOne value)
{
  fprintf(stderr, "Calling to_string for enum EOne with %d\n", (int)value);
  std::string name("One: ");
  name += '0'+(int)value;
  return name;
}

std::string to_string(ETwo value)
{
  fprintf(stderr, "Calling to_string for enum ETwo with %d\n", (int)value);
  std::string name("Two: ");
  name += '0'+(int)value;
  return name;
}

std::string to_string(EThree value)
{
  fprintf(stderr, "Calling to_string for enum EThree with %d\n", (int)value);
  std::string name("Three: ");
  name += '0'+(int)value;
  return name;
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions