Skip to content

Commit 0280e8d

Browse files
authored
Merge pull request #70 from robotpy/fwd-template-method
Allow forward declared template methods
2 parents cc47d67 + 4d16552 commit 0280e8d

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

cxxheaderparser/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1864,7 +1864,8 @@ def _parse_function(
18641864
self.visitor.on_class_method(state, method)
18651865
else:
18661866
assert isinstance(state, (ExternBlockState, NamespaceBlockState))
1867-
if not method.has_body:
1867+
# only template specializations can be declared without a body here
1868+
if not method.has_body and not method.template:
18681869
raise self._parse_error(None, expected="Method body")
18691870
self.visitor.on_method_impl(state, method)
18701871

tests/test_template.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,3 +1987,43 @@ def test_extern_template() -> None:
19871987
},
19881988
)
19891989
)
1990+
1991+
1992+
def test_fwd_declared_method() -> None:
1993+
content = """
1994+
// forward declaration for specialized template function
1995+
template <> Clazz<A>::Clazz();
1996+
"""
1997+
data = parse_string(content, cleandoc=True)
1998+
1999+
assert data == ParsedData(
2000+
namespace=NamespaceScope(
2001+
method_impls=[
2002+
Method(
2003+
return_type=None,
2004+
name=PQName(
2005+
segments=[
2006+
NameSpecifier(
2007+
name="Clazz",
2008+
specialization=TemplateSpecialization(
2009+
args=[
2010+
TemplateArgument(
2011+
arg=Type(
2012+
typename=PQName(
2013+
segments=[NameSpecifier(name="A")]
2014+
)
2015+
)
2016+
)
2017+
]
2018+
),
2019+
),
2020+
NameSpecifier(name="Clazz"),
2021+
]
2022+
),
2023+
parameters=[],
2024+
template=TemplateDecl(),
2025+
constructor=True,
2026+
)
2027+
]
2028+
)
2029+
)

0 commit comments

Comments
 (0)