Skip to content

Commit

Permalink
Added support for requires() constraints on methods that are not dire…
Browse files Browse the repository at this point in the history
…ctly templated
  • Loading branch information
justinboswell committed Dec 1, 2023
1 parent f1708bf commit ad6ec5b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cxxheaderparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ def _parse_method_end(self, method: Method) -> None:
elif tok_value == "requires":
method_template = method.template
if method_template is None:
raise self._parse_error(tok)
method_template = TemplateDecl([])
elif isinstance(method_template, list):
method_template = method_template[0]
method_template.raw_requires_post = self._parse_requires(tok)
Expand Down
55 changes: 55 additions & 0 deletions tests/test_concepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Concept,
Function,
FundamentalSpecifier,
Method,
MoveReference,
NameSpecifier,
PQName,
Expand Down Expand Up @@ -809,3 +810,57 @@ def test_requires_paren() -> None:
]
)
)


def test_non_template_requires() -> None:
content = """
template <class T>
struct Payload
{
constexpr Payload(T v)
requires(std::is_pod_v<T>)
: Value(v)
{
}
};
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="Payload")], classkey="struct"
),
template=TemplateDecl(
params=[TemplateTypeParam(
typekey="class", name="T")]
),
),
methods=[
Method(
return_type=None,
name=PQName(
segments=[NameSpecifier(name="Payload")]),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[NameSpecifier(name="T")]
)
),
name="v",
)
],
constexpr=True,
has_body=True,
access="public",
constructor=True,
)
],
)
]
)
)

0 comments on commit ad6ec5b

Please sign in to comment.