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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)