Skip to content

Commit fd68d4c

Browse files
committed
Test: Default Alias in Base Class
This tests that template type aliases for base class templates are resolved to the same registered types.
1 parent cf7d2e6 commit fd68d4c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ set(PYBIND11_TEST_FILES
153153
test_stl
154154
test_stl_binders
155155
test_tagbased_polymorphic
156+
test_template_alias_base
156157
test_thread
157158
test_union
158159
test_virtual_functions)

tests/test_template_alias_base.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "pybind11_tests.h"
2+
3+
#include <memory>
4+
5+
template <class T>
6+
using DefaultAllocator = std::allocator<T>;
7+
8+
template <template <class> class Allocator = DefaultAllocator>
9+
struct Base {};
10+
11+
template <template <class> class Allocator = DefaultAllocator>
12+
struct S : public Base<Allocator> {};
13+
14+
// this returns S<DefaultAllocator> even though we register
15+
// the type S<std::allocator>
16+
// MSVC and Clang on Windows failed here to detect the registered type
17+
S<> make_S() { return S<>{}; }
18+
19+
TEST_SUBMODULE(template_alias_base, m) {
20+
py::class_<Base<std::allocator>>(m, "B_std")
21+
.def(py::init());
22+
py::class_<S<std::allocator>, Base<std::allocator>>(m, "S_std")
23+
.def(py::init());
24+
25+
m.def("make_S", &make_S);
26+
}

tests/test_template_alias_base.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pybind11_tests import template_alias_base as m
2+
3+
4+
def test_can_create_variable():
5+
v = m.S_std()
6+
print(v)
7+
8+
9+
def test_can_return_variable():
10+
v = m.make_S()
11+
print(v)

0 commit comments

Comments
 (0)