Skip to content

Commit 92ead61

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 92ead61

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Diff for: 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)

Diff for: tests/test_template_alias_base.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
py::class_<S<std::allocator>, Base<std::allocator>>(m, "S_std");
22+
23+
m.def("make_S", &make_S);
24+
}

Diff for: tests/test_template_alias_base.py

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

0 commit comments

Comments
 (0)