{"action":"opened","issue":{"url":"https://api.github.com/repos/pybind/pybind11/issues/196","repository_url":"https://api.github.com/repos/pybind/pybind11","labels_url":"https://api.github.com/repos/pybind/pybind11/issues/196/labels{/name}","comments_url":"https://api.github.com/repos/pybind/pybind11/issues/196/comments","events_url":"https://api.github.com/repos/pybind/pybind11/issues/196/events","html_url":"https://github.com/pybind/pybind11/issues/196","id":154929310,"number":196,"title":"Questions about __init__, construction, undefined behaviour and exception safety","user":{"login":"bluescarni","id":623038,"avatar_url":"https://avatars.githubusercontent.com/u/623038?v=3","gravatar_id":"","url":"https://api.github.com/users/bluescarni","html_url":"https://github.com/bluescarni","followers_url":"https://api.github.com/users/bluescarni/followers","following_url":"https://api.github.com/users/bluescarni/following{/other_user}","gists_url":"https://api.github.com/users/bluescarni/gists{/gist_id}","starred_url":"https://api.github.com/users/bluescarni/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluescarni/subscriptions","organizations_url":"https://api.github.com/users/bluescarni/orgs","repos_url":"https://api.github.com/users/bluescarni/repos","events_url":"https://api.github.com/users/bluescarni/events{/privacy}","received_events_url":"https://api.github.com/users/bluescarni/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-15T20:47:19Z","updated_at":"2016-05-15T20:47:19Z","closed_at":null,"body":"I am struggling to understand exactly what is going on in snippets of code like this (taken from the documentation):\r\n\r\n```c++\r\npy::class_<Example>(m, \"Example\")\r\n    .def(\"__init__\",\r\n        [](Example &instance, int arg) {\r\n            new (&instance) Example(arg);\r\n        }\r\n    );\r\n```\r\n\r\nI understand that the intent here is to call placement new into the memory area of ``instance`` to construct the ``Example`` object with the provided argument ``arg``. My first question is: how is the reference ``Example &instance`` obtained? I am familiar with the use of, e.g., aligned storage + placement new on a ``void *`` after a couple of ``static_cast``s, but I am wondering how it has been implemented in pybind11 since it is using reference semantics.\r\n\r\nSecondly, what happens if the constructor throws? Imagine something like this:\r\n\r\n```c++\r\npy::class_<Example>(m, \"Example\")\r\n    .def(\"__init__\",\r\n        [](Example &instance, int arg) {\r\n            new (&instance) Example(arg);\r\n            throw std::runtime_error(\"\");\r\n        }\r\n    );\r\n```\r\n\r\nOr imagine even that the ``throw`` is placed before the placement new call. What is it going to happen in that case? Is pybind11 going to call the destructor of ``Example``? Can it detect if the C++ constructor has been called or not?\r\n\r\nThis seems to be a worse issue in the pickling support:\r\n\r\n```c++\r\n    .def(\"__setstate__\", [](Pickleable &p, py::tuple t) {\r\n        if (t.size() != 2)\r\n            throw std::runtime_error(\"Invalid state!\");\r\n\r\n        /* Invoke the in-place constructor. Note that this is needed even\r\n           when the object just has a trivial default constructor */\r\n        new (&p) Pickleable(t[0].cast<std::string>());\r\n\r\n        /* Assign any additional state */\r\n        p.setExtra(t[1].cast<int>());\r\n    });\r\n```\r\n\r\nHere the C++ constructor of ``Pickleable`` may or may not be called depending on the content of ``t``.\r\n\r\nAm I missing something obvious?"}}