matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

ambiguous call

Open bjornrommel opened this issue 3 years ago • 5 comments

When compiling a standard example from readthedocs,

#include #include "matplotlibcpp.h" namespace plt = matplotlibcpp; int main() { std::vector x = {1, 2, 3, 4}; std::vector y = {1, 4, 9, 16}; plt::plot(x, y,"r*"); plt::show(); }

on VisualStudio22 with the ISO C++20 flag and native Python 3.10, I get

Screenshot 2022-01-26 172910

Apparently, the compiler cannot decide between

// @brief standard plot function supporting the args (x, y, s, keywords) // line 519 // ... template <typename VectorX, typename VectorY> bool plot(const VectorX &x, const VectorY &y, const std::string &s = "", const std::map<std::string, std::string> &keywords = {}) { return detail::plot_base(detail::_interpreter::get().s_python_function_plot, x, y, s, keywords); }

and

// enable plotting of multiple triples (x, y, format) // line 1953 template <typename A, typename B, typename... Args> bool plot(const A &a, const B &b, const std::string &format, Args... args) { return plot(a, b, format) && plot(args...); }

As I understand, correct me if I'm wrong, there a several constructors, each of which is designed to fall through to the first one above, the one that finally calls plot_base. So, I renamed all constructors but the first one above plot1, and my code now successfully calls plt::plot1 ..... Of course, messing with your library and using non-standard calls on my side is not a proper solution. Rename the fall-through constructor, and I haven't even tested similarily overloaded functions?

bjornrommel avatar Jan 28 '22 11:01 bjornrommel

That's an interesting failure! It's been a while since I was actively using and maintaining this code, but I think the second signature in your comment is to allow chaining of multiple datasets e.g.

plot(x1, y1, "r*", x2, y2, "b--", ...);

I'm a little confused as to why the compilation fails for you, is there an option you can force the compiler to choose one of the options if there are multiple? Both would work in this case.

If that's not possible, I'd rename or remove the second function. It's just for convenience to allow multiple datasets in a single function call, I think it's justified (and probably cleaner too) to have one line per dataset.

Cryoris avatar Mar 17 '22 17:03 Cryoris

No, no chaining involved! The compiler cannot decide between the two overloaded alternatives because one of them defaults missing arguments.

Please, check which function is supposed to catch plot(x, y, string)? The first alternative catches plot(x, y, string, keyword), which works because keyword defaults to {}; so, that's valid. The second alternatives catches plot(x,y,string), which is also valid. Here, string and format are meant to refer to the same argument.

bjornrommel avatar Mar 20 '22 18:03 bjornrommel

Please, check which function is supposed to catch plot(x, y, string)?

The first one. The second one is only there to enable the chaining which you don't need it seems 🙂

Cryoris avatar Apr 26 '22 15:04 Cryoris

Sorry, I think, you still have to make the alternative overloads unique, but making each function call plot_base directly might be worth pursuing.

On Tue, 26 Apr 2022 at 17:17, Julien Gacon @.***> wrote:

Please, check which function is supposed to catch plot(x, y, string)?

The first one. The second one is only there to enable the chaining which you don't need it seems 🙂

— Reply to this email directly, view it on GitHub https://github.com/Cryoris/matplotlib-cpp/issues/3#issuecomment-1109926522, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVGR7TUWCNHIHA6R2RO32DTVHACH3ANCNFSM5NAR4AUA . You are receiving this because you authored the thread.Message ID: @.***>

bjornrommel avatar Apr 26 '22 16:04 bjornrommel

Yes, sure, but my compiler doesn't decide between equally valid alternatives! You can overload a function, but each definition must be unique, and they are not. You could either remove the ambiguity -- so, only one parameter list matches -- or make every function call plot_base if that's possible.

On Tue, 26 Apr 2022 at 17:17, Julien Gacon @.***> wrote:

Please, check which function is supposed to catch plot(x, y, string)?

The first one. The second one is only there to enable the chaining which you don't need it seems 🙂

— Reply to this email directly, view it on GitHub https://github.com/Cryoris/matplotlib-cpp/issues/3#issuecomment-1109926522, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVGR7TUWCNHIHA6R2RO32DTVHACH3ANCNFSM5NAR4AUA . You are receiving this because you authored the thread.Message ID: @.***>

bjornrommel avatar Oct 11 '22 08:10 bjornrommel