-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extra any_container constructor #2974
base: master
Are you sure you want to change the base?
Conversation
This lets us construct an array with py::array_t<double>({ 2L, 3 }); without needing to first cast everything to the same type.
Another constructor already uses single braces, although it has less risk of ambiguity. Another possibility would be to be explicit: std::initializer_list<T>{count}
|
I finally got around to running this PR through our global testing. Unfortunately I'm seeing hundreds of build breakages like this:
Taking that as a measure for how much trouble this will cause in the world at large, I think this change is too disruptive. I didn't think this through at all, sorry if this is naive: is there a way to not break the |
Ok, thanks for testing. AFAIU those warnings are harmless and only produced by some very old version of clang, but I understand they can be an issue for some users.
I'll think about it. In the mean time, let me switch this PR to draft (or maybe close it). |
I took a slightly closer look at the ~640 build failures. According to some tool we have, there are only 3 root causes, i.e. the build breakages may actually be easy to fix, at least internally (inside Google). External breakages are a different problem: the same bindings code would need to work with the pybind11 version we have internally (smart_holder branch, with is closely tracking master) and externally with a couple pybind11 releases back. Do you think that's doable? — I'm still having doubts about cost-vs-benefit in this particular case, but please let us know your own assessment. We are using a super-fresh version of clang internally (we follow LLVM upstream OSS very closely), with |
I don't think the current patch is worth the trouble, unless we can tweak it so it avoids this clang warning without modifying callers (I haven't had time to experiment with that). |
It seems that it is really by accident that clang does not warn with the current pybind11 code. #include <initializer_list>
struct A{
#ifdef WARN
A(std::initializer_list<int>);
#else
template<class T>A(std::initializer_list<T>);
#endif
};
void f(A){}
void g(int i){f({{i}});} Clang warns if I define WARN, but not otherwise. But T is deduced as int, so we are really doing the same thing in both cases. However, clang has
which disables the warning too broadly I think. (I didn't step through it, maybe the warning is disabled by something else) The double braces make perfect sense if we expect to use the constructor |
Cool :-)
My feeling is that explaining such a nuance in the documentation is easily more confusing than helpful, but a well-crafted concise comment in a unit test is pretty likely to work as intended. (That's where I look for nuances.) Could that make sense here? Do you want to reuse this PR for a small unit test addition? |
Description
This lets us construct an array with
without needing to first cast everything to the same type. Note that it will not allow mixed types if narrowing conversions are involved, but that's not what I am aiming for right now.
It is similar to #885, but that PR was replacing the template constructor with this one, while I am adding it as a complement. This avoids introducing narrowing errors, since those cases can keep using the old constructor.
(Obviously this isn't a priority, just a nice-to-have if it does not cause trouble elsewhere)