Cpp.CastExpression Class
Namespace: Cpp
Superclasses: AstNodeProperties
Represents the cast_expression nodes in the syntax tree of your code
Since R2026a
Description
The PQL class CastExpression represents the node cast_expression in the syntax tree of your code.
#include <iostream>
void test() {
int a = (int)3.14;
float b = (float)a;
char c = (char)42;
double d = (double)(a + b);
std::cout << a << ' ' << b << ' ' << c << ' ' << d << '\n';
}
int main() {
test();
return 0;
}The C++ demo contains several C-style cast expressions like (int)3.14 and (double)(a + b), which correspond to cast_expression nodes matched by the CastExpression PQL class.
Predicates
| Type | Raisable | Printable |
|---|---|---|
CastExpression
| 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 CastExpression &ce)
| Matches a cast_expression node and outputs it as
&ce. Use when you need the cast node itself. |
This PQL defect checks for any cast expression node in the code. defect find_casts =
when
Cpp.CastExpression.is(&ce)
and ce.nodeText(&txt)
raise "Found cast: \"{txt}\""
on ceIn this C++ code, the defect finds every C-style cast like
#include <iostream>
void f() {
int x = (int)3.14; //match
double y = (double)(x + 0.5);//match
(void)y; //match
}
int main(){ f(); return 0; } |
cast(Cpp.Node.Node node, required CastExpression &cast)
| Checks whether a given node is a cast_expression; if so, returns it as &cast for further inspection. |
This PQL defect checks whether an arbitrary node is a cast expression and then reports it. defect detect_cast_node =
when
Cpp.Node.is(&node, &,&,&)
and Cpp.CastExpression.cast(node, &ce)
and ce.nodeText(&txt)
raise "Node is a cast: \"{txt}\""
on ceIn this C++ code, the defect inspects generic syntax nodes and identifies the
void g() {
int a = 10;
float b = (float)a;
}
int main() { g(); return 0; } |
isa(Cpp.Node.Node node)
| Returns true if node is a
cast_expression. Use for boolean checks or negations. |
This PQL defect checks for nodes that are cast expressions using a boolean-style predicate. defect isa_cast_check =
when
Cpp.Node.is(&node, &,&,&)
and Cpp.CastExpression.isa(node)
and node.nodeText(&txt)
raise "isa detected cast node: \"{txt}\""
on nodeIn this C++ code, the defect discovers nodes like
void h() {
char c = (char)42;
}
int main(){ h(); return 0; } |
type(CastExpression self, Cpp.Node.Node &child)
| Matches the type portion of a cast expression. That is, in a cast expression
like (int)x, this predicate fetches the int
node and returns it as &child. |
This PQL defect checks for the type component inside cast expressions. defect cast_type_check =
when
Cpp.CastExpression.is(&ce)
and ce.type(&typeNode)
and typeNode.nodeText(&txt)
raise "Cast type is: \"{txt}\""
on ceIn this C++ code, the defect extracts the
void k() {
int i = (int)2.7;
double z = (double)i;
}
int main(){ k(); return 0; } |
value(CastExpression self, Cpp.Node.Node &child)
| Matches the expression being cast. That is, in a cast expression like
(int)x, this predicate fetches the x node
and returns it as &child | This PQL defect checks for the value part of cast expressions. defect cast_value_check =
when
Cpp.CastExpression.is(&ce)
and ce.value(&valNode)
and valNode.nodeText(&txt)
raise "Cast value is: \"{txt}\""
on ceIn this C++ code, the defect identifies
void m() {
int a = (int)3.14;
double d = (double)(a + 1.2);
}
int main(){ m(); return 0; } |
getEnclosingCastExpression(Cpp.Node.Node child, required CastExpression &parent)
| Finds the nearest enclosing cast_expression that contains the child node and returns it as &parent. | This PQL defect checks for the closest cast expression that surrounds a generic child node. defect find_enclosing_cast =
when
Cpp.Node.is(&inner, &,&,&)
and Cpp.CastExpression.getEnclosingCastExpression(inner, &ce)
and ce.nodeText(&txt)
raise "Enclosing cast: \"{txt}\""
on ceIn this C++ code, the defect finds the outer
void n() {
int a = 1;
float b = 2.0f;
double d = (double)(a + b);
(void)d;
}
int main(){ n(); return 0; } |
isEnclosedInCastExpression(Cpp.Node.Node child)
| True when child has a cast_expression as an ancestor; useful to filter nodes that appear inside casts. | This PQL defect checks whether a node is located inside any cast expression. defect inside_cast_check =
when
Cpp.Node.is(&node, &,&,&)
and Cpp.CastExpression.isEnclosedInCastExpression(node)
and node.nodeText(&txt)
raise "Node inside cast: \"{txt}\""
on nodeIn this C++ code, the defect finds all nodes that
are descendent of a
void p() {
int a = 1;
float b = 2.5f;
double d = (double)(a + b);
(void)d;
}
int main(){ p(); return 0; } |
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)