Resolve empty statement warning when using PYBIND11_OVERLOAD_PURE_NAME and PYBIND11_OVERLOAD_PURE (#2325)
* Wrap PYBIND11_OVERLOAD_NAME and PYBIND11_OVERLOAD_PURE_NAME in do { ... } while (false), and resolve trailing semicolon
* Deprecate PYBIND11_OVERLOAD_* and get_overload in favor of PYBIND11_OVERRIDE_* and get_override
* Correct erroneous usage of 'overload' instead of 'override' in the implementation and internals
* Fix tests to use non-deprecated PYBIND11_OVERRIDE_* macros
* Update docs to use override instead of overload where appropriate, and add warning about deprecated aliases
* Add semicolons to deprecated PYBIND11_OVERLOAD macros to match original behavior
* Remove deprecation of PYBIND11_OVERLOAD_* macros and get_overload
* Add note to changelog and upgrade guide
diff --git a/tests/test_class.cpp b/tests/test_class.cpp
index b7d52a1..e7eaa83 100644
--- a/tests/test_class.cpp
+++ b/tests/test_class.cpp
@@ -317,7 +317,7 @@
class TrampolineB : public ProtectedB {
public:
- int foo() const override { PYBIND11_OVERLOAD(int, ProtectedB, foo, ); }
+ int foo() const override { PYBIND11_OVERRIDE(int, ProtectedB, foo, ); }
};
class PublicistB : public ProtectedB {
diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp
index 222bd56..753ce54 100644
--- a/tests/test_embed/test_interpreter.cpp
+++ b/tests/test_embed/test_interpreter.cpp
@@ -30,7 +30,7 @@
class PyWidget final : public Widget {
using Widget::Widget;
- int the_answer() const override { PYBIND11_OVERLOAD_PURE(int, Widget, the_answer); }
+ int the_answer() const override { PYBIND11_OVERRIDE_PURE(int, Widget, the_answer); }
};
PYBIND11_EMBEDDED_MODULE(widget_module, m) {
diff --git a/tests/test_factory_constructors.cpp b/tests/test_factory_constructors.cpp
index f614743..f70ed33 100644
--- a/tests/test_factory_constructors.cpp
+++ b/tests/test_factory_constructors.cpp
@@ -89,7 +89,7 @@
PyTF6(const PyTF6 &f) : TestFactory6(f) { print_copy_created(this); }
PyTF6(std::string s) : TestFactory6((int) s.size()) { alias = true; print_created(this, s); }
virtual ~PyTF6() { print_destroyed(this); }
- int get() override { PYBIND11_OVERLOAD(int, TestFactory6, get, /*no args*/); }
+ int get() override { PYBIND11_OVERRIDE(int, TestFactory6, get, /*no args*/); }
};
class TestFactory7 {
@@ -110,7 +110,7 @@
PyTF7(PyTF7 &&f) : TestFactory7(std::move(f)) { print_move_created(this); }
PyTF7(const PyTF7 &f) : TestFactory7(f) { print_copy_created(this); }
virtual ~PyTF7() { print_destroyed(this); }
- int get() override { PYBIND11_OVERLOAD(int, TestFactory7, get, /*no args*/); }
+ int get() override { PYBIND11_OVERRIDE(int, TestFactory7, get, /*no args*/); }
};
diff --git a/tests/test_gil_scoped.cpp b/tests/test_gil_scoped.cpp
index dc9b7ed..eb63089 100644
--- a/tests/test_gil_scoped.cpp
+++ b/tests/test_gil_scoped.cpp
@@ -22,10 +22,10 @@
class PyVirtClass : public VirtClass {
void virtual_func() override {
- PYBIND11_OVERLOAD(void, VirtClass, virtual_func,);
+ PYBIND11_OVERRIDE(void, VirtClass, virtual_func,);
}
void pure_virtual_func() override {
- PYBIND11_OVERLOAD_PURE(void, VirtClass, pure_virtual_func,);
+ PYBIND11_OVERRIDE_PURE(void, VirtClass, pure_virtual_func,);
}
};
diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp
index c5ee636..0210695 100644
--- a/tests/test_virtual_functions.cpp
+++ b/tests/test_virtual_functions.cpp
@@ -47,7 +47,7 @@
int run(int value) override {
/* Generate wrapping code that enables native function overloading */
- PYBIND11_OVERLOAD(
+ PYBIND11_OVERRIDE(
int, /* Return type */
ExampleVirt, /* Parent class */
run, /* Name of function */
@@ -56,7 +56,7 @@
}
bool run_bool() override {
- PYBIND11_OVERLOAD_PURE(
+ PYBIND11_OVERRIDE_PURE(
bool, /* Return type */
ExampleVirt, /* Parent class */
run_bool, /* Name of function */
@@ -66,7 +66,7 @@
}
void pure_virtual() override {
- PYBIND11_OVERLOAD_PURE(
+ PYBIND11_OVERRIDE_PURE(
void, /* Return type */
ExampleVirt, /* Parent class */
pure_virtual, /* Name of function */
@@ -78,7 +78,7 @@
// We can return reference types for compatibility with C++ virtual interfaces that do so, but
// note they have some significant limitations (see the documentation).
const std::string &get_string1() override {
- PYBIND11_OVERLOAD(
+ PYBIND11_OVERRIDE(
const std::string &, /* Return type */
ExampleVirt, /* Parent class */
get_string1, /* Name of function */
@@ -87,7 +87,7 @@
}
const std::string *get_string2() override {
- PYBIND11_OVERLOAD(
+ PYBIND11_OVERRIDE(
const std::string *, /* Return type */
ExampleVirt, /* Parent class */
get_string2, /* Name of function */
@@ -141,11 +141,11 @@
class NCVirtTrampoline : public NCVirt {
#if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__)
NonCopyable get_noncopyable(int a, int b) override {
- PYBIND11_OVERLOAD(NonCopyable, NCVirt, get_noncopyable, a, b);
+ PYBIND11_OVERRIDE(NonCopyable, NCVirt, get_noncopyable, a, b);
}
#endif
Movable get_movable(int a, int b) override {
- PYBIND11_OVERLOAD_PURE(Movable, NCVirt, get_movable, a, b);
+ PYBIND11_OVERRIDE_PURE(Movable, NCVirt, get_movable, a, b);
}
};
@@ -159,7 +159,7 @@
struct DispatchIssue : Base {
virtual std::string dispatch() const {
- PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
+ PYBIND11_OVERRIDE_PURE(std::string, Base, dispatch, /* no arguments */);
}
};
@@ -240,7 +240,7 @@
py::print("PyA.f()");
// This convolution just gives a `void`, but tests that PYBIND11_TYPE() works to protect
// a type containing a ,
- PYBIND11_OVERLOAD(PYBIND11_TYPE(typename std::enable_if<true, void>::type), A, f);
+ PYBIND11_OVERRIDE(PYBIND11_TYPE(typename std::enable_if<true, void>::type), A, f);
}
};
@@ -265,7 +265,7 @@
~PyA2() { py::print("PyA2.~PyA2()"); }
void f() override {
py::print("PyA2.f()");
- PYBIND11_OVERLOAD(void, A2, f);
+ PYBIND11_OVERRIDE(void, A2, f);
}
};
@@ -304,19 +304,19 @@
class PyOverrideTest : public OverrideTest {
public:
using OverrideTest::OverrideTest;
- std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
+ std::string str_value() override { PYBIND11_OVERRIDE(std::string, OverrideTest, str_value); }
// Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
// to a python numeric value, since we only copy values in the numeric type caster:
-// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
+// std::string &str_ref() override { PYBIND11_OVERRIDE(std::string &, OverrideTest, str_ref); }
// But we can work around it like this:
private:
std::string _tmp;
- std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
+ std::string str_ref_helper() { PYBIND11_OVERRIDE(std::string, OverrideTest, str_ref); }
public:
std::string &str_ref() override { return _tmp = str_ref_helper(); }
- A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
- A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
+ A A_value() override { PYBIND11_OVERRIDE(A, OverrideTest, A_value); }
+ A &A_ref() override { PYBIND11_OVERRIDE(A &, OverrideTest, A_ref); }
};
py::class_<OverrideTest::A>(m, "OverrideTest_A")
@@ -393,29 +393,29 @@
class PyA_Repeat : public A_Repeat {
public:
using A_Repeat::A_Repeat;
- int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, A_Repeat, unlucky_number, ); }
- std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, A_Repeat, say_something, times); }
+ int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, A_Repeat, unlucky_number, ); }
+ std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, A_Repeat, say_something, times); }
};
class PyB_Repeat : public B_Repeat {
public:
using B_Repeat::B_Repeat;
- int unlucky_number() override { PYBIND11_OVERLOAD(int, B_Repeat, unlucky_number, ); }
- std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, B_Repeat, say_something, times); }
- double lucky_number() override { PYBIND11_OVERLOAD(double, B_Repeat, lucky_number, ); }
+ int unlucky_number() override { PYBIND11_OVERRIDE(int, B_Repeat, unlucky_number, ); }
+ std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, B_Repeat, say_something, times); }
+ double lucky_number() override { PYBIND11_OVERRIDE(double, B_Repeat, lucky_number, ); }
};
class PyC_Repeat : public C_Repeat {
public:
using C_Repeat::C_Repeat;
- int unlucky_number() override { PYBIND11_OVERLOAD(int, C_Repeat, unlucky_number, ); }
- std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, C_Repeat, say_something, times); }
- double lucky_number() override { PYBIND11_OVERLOAD(double, C_Repeat, lucky_number, ); }
+ int unlucky_number() override { PYBIND11_OVERRIDE(int, C_Repeat, unlucky_number, ); }
+ std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, C_Repeat, say_something, times); }
+ double lucky_number() override { PYBIND11_OVERRIDE(double, C_Repeat, lucky_number, ); }
};
class PyD_Repeat : public D_Repeat {
public:
using D_Repeat::D_Repeat;
- int unlucky_number() override { PYBIND11_OVERLOAD(int, D_Repeat, unlucky_number, ); }
- std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, D_Repeat, say_something, times); }
- double lucky_number() override { PYBIND11_OVERLOAD(double, D_Repeat, lucky_number, ); }
+ int unlucky_number() override { PYBIND11_OVERRIDE(int, D_Repeat, unlucky_number, ); }
+ std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, D_Repeat, say_something, times); }
+ double lucky_number() override { PYBIND11_OVERRIDE(double, D_Repeat, lucky_number, ); }
};
// Inheritance approach 2: templated trampoline classes.
@@ -436,15 +436,15 @@
class PyA_Tpl : public Base {
public:
using Base::Base; // Inherit constructors
- int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, Base, unlucky_number, ); }
- std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, Base, say_something, times); }
+ int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, Base, unlucky_number, ); }
+ std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, Base, say_something, times); }
};
template <class Base = B_Tpl>
class PyB_Tpl : public PyA_Tpl<Base> {
public:
using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors)
- int unlucky_number() override { PYBIND11_OVERLOAD(int, Base, unlucky_number, ); }
- double lucky_number() override { PYBIND11_OVERLOAD(double, Base, lucky_number, ); }
+ int unlucky_number() override { PYBIND11_OVERRIDE(int, Base, unlucky_number, ); }
+ double lucky_number() override { PYBIND11_OVERRIDE(double, Base, lucky_number, ); }
};
// Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these (we can
// use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead):