Skip to content

Commit

Permalink
Support deduction guides with sizeof in template expression
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Oct 26, 2024
1 parent 5ed4baf commit ed935fd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
6 changes: 1 addition & 5 deletions cxxheaderparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,7 @@ def _parse_template_specialization(self) -> TemplateSpecialization:
args.append(TemplateArgument(dtype, param_pack))
else:
# special case for sizeof...(thing)
if (
param_pack
and len(val.tokens) == 1
and val.tokens[0].value == "sizeof"
):
if param_pack and val.tokens[-1].value == "sizeof":
val.tokens.append(Token("...", "ELLIPSIS"))
tok = self._next_token_must_be("(")
raw_toks = self._consume_balanced_tokens(tok)
Expand Down
65 changes: 65 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2352,3 +2352,68 @@ def test_sizeof_pack() -> None:
]
)
)


def test_deduction_sizeof_pack() -> None:
content = """
template <typename ModuleTranslation, typename... ModuleTranslations>
SwerveDriveKinematics(ModuleTranslation, ModuleTranslations...)
-> SwerveDriveKinematics<1 + sizeof...(ModuleTranslations)>;
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
deduction_guides=[
DeductionGuide(
result_type=Type(
typename=PQName(
segments=[
NameSpecifier(
name="SwerveDriveKinematics",
specialization=TemplateSpecialization(
args=[
TemplateArgument(
arg=Value(
tokens=[
Token(value="1"),
Token(value="+"),
Token(value="sizeof"),
Token(value="..."),
Token(value="("),
Token(
value="ModuleTranslations"
),
Token(value=")"),
]
),
param_pack=True,
)
]
),
)
]
)
),
name=PQName(segments=[NameSpecifier(name="SwerveDriveKinematics")]),
parameters=[
Parameter(
type=Type(
typename=PQName(
segments=[NameSpecifier(name="ModuleTranslation")]
)
)
),
Parameter(
type=Type(
typename=PQName(
segments=[NameSpecifier(name="ModuleTranslations")]
)
),
param_pack=True,
),
],
)
]
)
)

0 comments on commit ed935fd

Please sign in to comment.