Skip to content

Commit ce7024f

Browse files
committed
Fix linker issue with move constructors on MSVC
Fixes the issue as described in the comments of commit e27ea47. This just adds `enable_if_t<std::is_move_constructible<T>::value>` to `make_move_constructor`. The change fixes MSVC and is harmless with other compilers.
1 parent 24dec80 commit ce7024f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

include/pybind11/cast.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,23 @@ template <typename type> class type_caster_base : public type_caster_generic {
774774
operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); }
775775

776776
protected:
777-
typedef void *(*Constructor)(const void *stream);
777+
using Constructor = void *(*)(const void *);
778+
778779
/* Only enabled when the types are {copy,move}-constructible *and* when the type
779-
does not have a private operator new implementaton. */
780-
template <typename T = type, typename = enable_if_t<is_copy_constructible<T>::value>> static auto make_copy_constructor(const T *value) -> decltype(new T(*value), Constructor(nullptr)) {
781-
return [](const void *arg) -> void * { return new T(*((const T *) arg)); }; }
782-
template <typename T = type> static auto make_move_constructor(const T *value) -> decltype(new T(std::move(*((T *) value))), Constructor(nullptr)) {
783-
return [](const void *arg) -> void * { return (void *) new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg)))); }; }
780+
does not have a private operator new implementation. */
781+
template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
782+
static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) {
783+
return [](const void *arg) -> void * {
784+
return new T(*reinterpret_cast<const T *>(arg));
785+
};
786+
}
787+
788+
template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
789+
static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*(T *) x)), Constructor{}) {
790+
return [](const void *arg) -> void * {
791+
return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
792+
};
793+
}
784794

785795
static Constructor make_copy_constructor(...) { return nullptr; }
786796
static Constructor make_move_constructor(...) { return nullptr; }

0 commit comments

Comments
 (0)