Skip to content

Commit cb51ef4

Browse files
committed
Wrap PYBIND11_OVERLOAD_NAME and PYBIND11_OVERLOAD_PURE_NAME in do { ... } while (false), and resolve trailing semicolon
1 parent 7e0a4fb commit cb51ef4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

include/pybind11/pybind11.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,8 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
21272127
return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
21282128
}
21292129

2130-
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
2130+
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) \
2131+
do { \
21312132
pybind11::gil_scoped_acquire gil; \
21322133
pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
21332134
if (overload) { \
@@ -2138,7 +2139,7 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
21382139
} \
21392140
else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
21402141
} \
2141-
}
2142+
} while (false)
21422143

21432144
/** \rst
21442145
Macro to populate the virtual method in the trampoline class. This macro tries to look up a method named 'fn'
@@ -2158,16 +2159,20 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
21582159
}
21592160
\endrst */
21602161
#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
2161-
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \
2162-
return cname::fn(__VA_ARGS__)
2162+
do { \
2163+
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
2164+
return cname::fn(__VA_ARGS__); \
2165+
} while (false)
21632166

21642167
/** \rst
21652168
Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERLOAD_NAME`, except that it
21662169
throws if no overload can be found.
21672170
\endrst */
21682171
#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
2169-
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \
2170-
pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\"");
2172+
do { \
2173+
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
2174+
pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); \
2175+
} while (false)
21712176

21722177
/** \rst
21732178
Macro to populate the virtual method in the trampoline class. This macro tries to look up the method

0 commit comments

Comments
 (0)