Cpp.FoldExpression Class
Namespace: Cpp
Superclasses: AstNodeProperties
Represents the fold_expression nodes in the syntax tree of your
code
Since R2026a
Description
The PQL class Cpp.FoldExpression represents the node
fold_expression in the syntax tree of your code.
template<typename... Args>
auto sum(Args... args) {
return (... + args); //fold_expression
template<typename... Args>
auto sum_with_init(Args... args) {
return (0 + ... + args); //fold_expression
}
template<typename... Args>
auto sum_right(Args... args) {
return (args + ... + 42); //fold_expression
}The three return statements contain fold_expression nodes. Each
corresponds to a Cpp.FoldExpression in PQL.
Predicates
| Type | Raisable | Printable |
|---|---|---|
FoldExpression
| Yes | No |
This class defines these predicates that act on the objects of this class. In addition,
objects of this class can access the predicates defined by the base class AstNodeProperties. An object of this class is an object of AstNodeProperties class.
| Predicates | Description | Example |
|---|---|---|
is(required FoldExpression &fe)
| Matches a fold_expression node and binds it to
fe. Use this to start inspecting fold expression
components. | This PQL defect checks for any defect findFold =
when
Cpp.FoldExpression.is(&fe)
and fe.nodeText(&txt)
raise "found fold: \"{txt}\""
on feIn this C++ code the defect finds each fold expression and reports its source text.
template<typename... Args>
auto sum(Args... args) { return (... + args); } |
cast(Cpp.Node.Node node, required FoldExpression &cast)
| Checks whether a Cpp.Node.Node is actually a
fold_expression and if so casts it to
FoldExpression. | This PQL defect checks for a defect castFold =
when
Cpp.ReturnStatement.is(&ret)
and ret.getADescendant(&desc_node)
and Cpp.FoldExpression.cast(desc_node, &fe)
and fe.nodeText(&txt)
raise "cast to FoldExpression: \"{txt}\""
on feIn this C++ code the defect finds the child nodes of a return expression and finds the node that can be cast to a fold expression.
#include <iostream>
template<typename... Args>
auto sum(Args... args) {
return (... + args); // defect
}
int main() {
// Example usage of the sum function - Bugfinder ignored unused function templates
auto result1 = sum(1, 2, 3, 4, 5);
auto result2 = sum(1.5, 2.5, 3.0);
std::cout << "Sum of integers: " << result1 << std::endl;
std::cout << "Sum of doubles: " << result2 << std::endl;
return 0;
}
|
isa(Cpp.Node.Node node)
| Checks whether a Cpp.Node.Node is a
fold_expression without producing a
FoldExpression binding. Use when you only need a boolean
test. | This PQL defect checks whether a generic child defect isaFold =
when
Cpp.ReturnStatement.is(&ret)
and ret.getADescendant(&desc_node)
and Cpp.FoldExpression.isa(desc_node)
raise "CHild node of a return statement is a fold_expression"
on retIn this C++ code the defect tests if the return expression is a fold expression.
template<typename... Args>
auto sum(Args... args) { return (... + args); } |
operator(FoldExpression self, Cpp.Node.Node &child)
| Matches the operator token used in the fold expression and binds that operator
token node to child. | This PQL defect checks for the operator token inside a
defect foldOperator =
when
Cpp.FoldExpression.is(&fe)
and fe.operator(&op)
and op.nodeText(&txt)
raise "fold operator: \"{txt}\""
on feIn this C++ code the defect extracts and reports the
fold operator such as
template<typename... Args>
auto sum(Args... args) { return (... + args); } |
left(FoldExpression self, Cpp.Node.Node &child)
| Binds the left operand of the fold expression to child. This
is the initial value in left-anchored forms. | This PQL defect checks for the left operand node of a
defect foldLeft =
when
Cpp.FoldExpression.is(&fe)
and fe.left(&left)
and left.nodeText(&txt)
raise "fold left: \"{txt}\""
on feIn this C++ code the defect finds the left initial value in a fold with an explicit initializer.
template<typename... Args>
auto sum_with_init(Args... args) { return (0 + ... + args); } |
right(FoldExpression self, Cpp.Node.Node &child)
| Binds the right operand of the fold expression to child.
This is the trailing value in right-anchored forms. | This PQL defect checks for the right operand node of a
defect foldRight =
when
Cpp.FoldExpression.is(&fe)
and fe.right(&r)
and r.nodeText(&txt)
raise "fold right: \"{txt}\""
on feIn this C++ code the defect locates the rightmost operand such as a constant on the right side.
template<typename... Args>
auto sum_right(Args... args) { return (args + ... + 42); } |
getEnclosingFoldExpression(Cpp.Node.Node child, required FoldExpression
&parent)
| Finds the nearest enclosing fold_expression ancestor of
child and binds it to parent. | This PQL defect checks which defect enclosingFold =
when
Cpp.Identifier.is(&id)
and id.toNode(&n)
and Cpp.FoldExpression.getEnclosingFoldExpression(n, &fe)
and fe.nodeText(&txt)
raise "enclosing fold: \"{txt}\" for identifier"
on idIn this C++ code the defect finds the fold expression that contains a specific identifier.
template<typename... Args>
auto sum(Args... args) { return (... + args); } |
isEnclosedInFoldExpression(Cpp.Node.Node child)
| Checks whether a Node has a
fold_expression ancestor. Use for boolean containment
tests. | This PQL defect checks whether a converted defect isInFold =
when
Cpp.Identifier.is(&id)
and id.toNode(&n)
and Cpp.FoldExpression.isEnclosedInFoldExpression(n)
raise "identifier is inside a fold_expression"
on idIn this C++ code the defect reports if an identifier is contained within a fold expression.
template<typename... Args>
auto sum(Args... args) {
return (... + args); // identifier usage inside fold not present here but pattern stands
} |
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)