Skip to content

Pythonization of list to std::vector fails with function template #18838

Open
@vepadulano

Description

@vepadulano

Check duplicate issues.

  • Checked for duplicates

Description

Automatic conversion of a list[tuple[int, int]] to std::vector<std::pair<int, int>> fails in case the function called is a function template. It works in the case of a free function:

$: python repro.py
Using free function:
{(0, 2), (4, 7), (8, 9), }
Using function template:
Traceback (most recent call last):
  File "/Users/vpadulan/Programs/rootproject/rootcode/repro.py", line 28, in <module>
    ROOT.print_template(33., a)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
TypeError: Template method resolution failed:
  Failed to instantiate "print_template(double,std::initializer_list<std::initializer_list<int>>)"

Reproducer

import ROOT

ROOT.gInterpreter.Declare(r"""
template<typename T>
void print_template(T dummy, const std::vector<std::pair<int, int>> &vec)
{
    std::cout << "{";
    for (const auto &pair: vec)
        std::cout << "(" << pair.first << ", " << pair.second << "), ";
    std::cout << "}\n";
}

void print_free_function(float dummy, const std::vector<std::pair<int, int>> &vec)
{
    std::cout << "{";
    for (const auto &pair: vec)
        std::cout << "(" << pair.first << ", " << pair.second << "), ";
    std::cout << "}\n";
}
""")

a = [(0, 2), (4, 7), (8, 9)]

print("Using free function:")
ROOT.print_free_function(33., a)

print("Using function template:")
ROOT.print_template(33., a)

ROOT version

Any

Installation method

Any

Operating system

Any

Additional context

No response

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions