Cpp.Cast Class
Namespace: Cpp
Superclasses: ObjectWithPosition
Description
Cast class represents a cast in your code. This class inherits from the
class ObjectWithPosition. You can use the predicates associated with this class and
the base class with objects of this class.
Predicates
You can report a defect on Raisable types. If a type is
Printable, it can be reported in the message. For
Cast objects, print identifiers such as column number, line
number, source file or source paths in the message.
Cast
| The class types defines this type. An object of type
Cast | Yes | No |
Lang.Int
| Signed Integer types | No | Yes |
Lang.String
| String type | No | Yes |
This class defines these predicates that act on the Cast objects.
In addition, objects of Cast class can access the predicates defined
by the base class ObjectWithPosition. An object of Cast class is an object
of ObjectWithPosition class.
| Predicate | Description | Example |
|---|---|---|
is(Cast &cast)
| Retrieves all Cast objects from your C/C++
code. |
This defect flags all casts in the C/C++ code you analyze: defect FlagAllCasts = when Cpp.Cast.is(&cast) raise "Cast detected" on cast |
isImplicit(Cast self)
| Retrieves all Cast objects from your C/C++ code that
perform implicit casts. |
This defect flags all implicit casts in the C/C++ code you analyze: defect FlagImplicitCast =
when Cpp.Cast.is(&cast)
and cast.isImplicit()
raise "Implicit Cast detected"
on cast |
isExplicit(Cast self)
| Retrieves all Cast objects from your C/C++ code that
perform explicit casts. |
This defect flags all explicit casts in the C/C++ code you analyze: defect FlagExplicitCast =
when Cpp.Cast.is(&cast)
and cast.isExplicit()
raise "Explicit Cast detected"
on cast |
fromType(Cast self, Cpp.Type.Type
&fromType)
| Retrieves all Cast objects from your C/C++ code that
perform cast from the type from. |
This defect flags all cast from the type defect FlagCastfromint =
when Cpp.Cast.is(&cast)
and cast.fromType(&fromType)
and fromType.toString(&typestr)
and typestr == "int"
raise "Cast from int detected"
on cast |
toType(Cast self, Cpp.Type.Type &to)
| Retrieves all Cast objects from your C/C++ code that
perform cast to the type to. |
This defect flags all cast to the type defect FlagCast2int =
when Cpp.Cast.is(&cast)
and cast.toType(&toType)
and toType.toString(&typestr)
and typestr == "int"
raise "Cast to int detected"
on cast |
precisionLoss(Cast self)
| Retrieves all Cast objects from your C/C++ code that
casts objects to a narrower type, leading to precision loss. |
This defect flags all casts in your C/C++ code that results in
loss of precision. Typically, cast to a narrower type results in
precision loss, such as cast from integer to
defect Flaglossycast =
when Cpp.Cast.is(&cast)
and cast.precisionLoss()
raise "Cast with precision loss detected"
on cast |
undefinedBehavior(Cast self)
| Retrieves all Cast objects from your C/C++ code that
leads to undefined behavior. |
This defect flags all casts in your C/C++ code that results in undefined behavior. For example, casts between pointers to unrelated classes. defect FlagUBCast =
when Cpp.Cast.is(&cast)
and cast.undefinedBehavior()
raise "Cast from int detected"
on cast |
Examples
In a new folder
Cast, initialize a new coding standard. At the command line, enter:polyspace-query-language init
In the file
main.pql, enter this content:package main // Main PQL file defines the catalog of your PQL project. // The catalog is a collection of sections. catalog CastExample = { #[Description("Example Section")] section ExampleSection = { #[Description("Explicit cast at the beginning"),Id(myRule)] rule ExampleRule = { defect Exampledefect = when Cpp.Cast.is(&cast) and cast.isExplicit() and cast.extension(&ext) and ext == ".cpp" and cast.line(&line) and line <6 raise "Unexpected explicit cast detected :\"{ext}\" \"{line}\"" on cast } } }Create the coding standard
Cast.pschk using this command at the command line:polyspace-query-language package
Using the coding standard, run a Bug Finder analysis on your source file. Foe example, at the command line, enter:
The analysis reports defects on the explicit casts that happen before the sixth line of the code.polyspace-bug-finder -sources src.cpp -lang cpp -checkers-activation-file Cast.pschk
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)