-
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
Brace initialization for simple POD classes #1015
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Making simple structs simpler.
This is a minor corner case, but since initializer_list
takes priority over regular constructors when brace initialization is used (the std::vector<int>
issue), somebody somewhere may be affected when upgrading. Might be worth mentioning in the docs.
tests/test_class.cpp
Outdated
@@ -229,6 +229,16 @@ TEST_SUBMODULE(class_, m) { | |||
// This test is actually part of test_local_bindings (test_duplicate_local), but we need a | |||
// definition in a different compilation unit within the same module: | |||
bind_local<LocalExternal, 17>(m, "LocalExternal", py::module_local()); | |||
|
|||
struct BraceInitialization { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick comment above this line: // test_brace_initializaton
Having the name of the Python test function as a comment above the corresponding C++ code makes it very convenient to navigate from a failed test function to the C++ source.
done. |
I deliberately keep the fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push upstream HEAD:master
To push to the branch of the same name on the remote, use
git push upstream upstream so that I can still push it using the first suggested command, but it's always an extra safety step (even against ordinary pushes, not just forced pushes). |
docs/advanced/classes.rst
Outdated
.def(py::init<int, const std::string &>()); | ||
|
||
Note that brace initialization preferentially invokes constructor overloads | ||
taking a ``std::initializer_list``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add:
In the rare event that this causes an issue, you can work around it by using py::init(...)
with a lambda function that constructs the new object as desired.
Since that makes it more than just a single sentence, it's probably useful to put it in a note
section.
One small suggested addition to the docs, but otherwise this LGTM. |
docs/advanced/classes.rst
Outdated
|
||
.. code-block:: cpp | ||
|
||
struct POD { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a POD (std::string
is not trivially copyable). Aggregate
should be the term for this kind of structure and brace initialization.
Rebased and squashed down into 2 commits. Unrelated: I noticed a number of warnings when compiling the test suite on Clang/OSX -- at least one of them is related to the new factory constructors.
|
Which version of clang? I'm not seeing these with appleclang v8.1.0. (In fact, seems to be reporting |
It's clang trunk compiled from SVN :). |
merged. |
For simple POD classes storing just a few fields, I often don't bother writing constructors and use the generalized C++11 brace initialization syntax. pybind11 doesn't like that and currently requires one to add constructors to the POD class (the
py::init
details use the C++03 syntax). This commit switches the pybind11 implementation to use brace initialization as well.^@jagerman
^@dean0x7d
Unrelated: I almost did a force push to the master branch instead of my personal repo but noticed in the last moment. I switched the master branch to protected status now (i.e. force pushes not allowed) to prevent accidents -- it's easily removed temporarily if we ever need to.